Command to print last N log messages from the default log.

This commit is contained in:
Daniil Fedotov 2019-06-26 15:56:22 -04:00
parent 536621ed1f
commit ff0372200a
4 changed files with 127 additions and 40 deletions

View File

@ -0,0 +1,61 @@
## The contents of this file are subject to the Mozilla Public License
## Version 1.1 (the "License"); you may not use this file except in
## compliance with the License. You may obtain a copy of the License
## at https://www.mozilla.org/MPL/
##
## Software distributed under the License is distributed on an "AS IS"
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
## the License for the specific language governing rights and
## limitations under the License.
##
## The Original Code is RabbitMQ.
##
## The Initial Developer of the Original Code is GoPivotal, Inc.
## Copyright (c) 2019 Pivotal Software, Inc. All rights reserved.
defmodule RabbitMQ.CLI.Core.LogFiles do
@spec get_log_locations(atom, integer | :infinity) :: [String.t] | {:badrpc, term}
def get_log_locations(node_name, timeout) do
case :rabbit_misc.rpc_call(node_name,
:rabbit_lager, :log_locations, [],
timeout) do
{:badrpc, _} = error -> error;
list -> Enum.map(list, &to_string/1)
end
end
@spec get_default_log_location(atom, integer | :infinity) ::
{:ok, String.t} | {:badrpc, term} | {:error, term}
def get_default_log_location(node_name, timeout) do
case get_log_locations(node_name, timeout) do
{:badrpc, _} = error -> error;
[] -> {:error, "No log files configured on the node"};
[first_log | _] = log_locations ->
case get_log_config_file_location(node_name, timeout) do
{:badrpc, _} = error -> error;
nil -> {:ok, first_log};
location ->
case Enum.member?(log_locations, location) do
true -> {:ok, to_string(location)};
## Configured location was not propagated to lager?
false -> {:ok, first_log}
end
end
end
end
defp get_log_config_file_location(node_name, timeout) do
case :rabbit_misc.rpc_call(node_name,
:application, :get_env, [:rabbit, :log, :none],
timeout) do
{:badrpc, _} = error -> error;
:none -> nil;
log_config ->
case log_config[:file] do
nil -> nil;
file_config ->
file_config[:file]
end
end
end
end

View File

@ -19,6 +19,8 @@ defmodule RabbitMQ.CLI.Diagnostics.Commands.LogLocationCommand do
"""
@behaviour RabbitMQ.CLI.CommandBehaviour
alias RabbitMQ.CLI.Core.LogFiles
def switches, do: [all: :boolean]
def aliases, do: [a: :all]
@ -29,44 +31,9 @@ defmodule RabbitMQ.CLI.Diagnostics.Commands.LogLocationCommand do
use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments
def run([], %{node: node_name, timeout: timeout, all: all}) do
case :rabbit_misc.rpc_call(node_name,
:rabbit_lager, :log_locations, [],
timeout) do
{:badrpc, _} = error ->
error;
## No log files?
[] -> {:error, "No log files configured on the node"};
[first_log | _] = log_locations ->
case all do
true -> log_locations;
false ->
## Select the "main" file
case get_log_config_file_location(node_name, timeout) do
{:badrpc, _} = error -> error;
nil -> to_string(first_log);
location ->
case Enum.member?(log_locations, location) do
true -> to_string(location);
## Configured location was not propagated to lager?
false -> to_string(first_log)
end
end
end
end
end
def get_log_config_file_location(node_name, timeout) do
case :rabbit_misc.rpc_call(node_name,
:application, :get_env, [:rabbit, :log, :none],
timeout) do
{:badrpc, _} = error -> error;
:none -> nil;
log_config ->
case log_config[:file] do
nil -> nil;
file_config ->
file_config[:file]
end
case all do
true -> LogFiles.get_log_locations(node_name, timeout);
false -> LogFiles.get_default_log_location(node_name, timeout)
end
end
@ -76,7 +43,7 @@ defmodule RabbitMQ.CLI.Diagnostics.Commands.LogLocationCommand do
def description(), do: "Shows the log file location(s) on the target node"
def usage, do: "log_location"
def usage, do: "log_location [--all|-a]"
def banner([], %{node: node_name}) do
"Log file location on node #{node_name} ..."

View File

@ -0,0 +1,59 @@
## The contents of this file are subject to the Mozilla Public License
## Version 1.1 (the "License"); you may not use this file except in
## compliance with the License. You may obtain a copy of the License
## at https://www.mozilla.org/MPL/
##
## Software distributed under the License is distributed on an "AS IS"
## basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
## the License for the specific language governing rights and
## limitations under the License.
##
## The Original Code is RabbitMQ.
##
## The Initial Developer of the Original Code is GoPivotal, Inc.
## Copyright (c) 2019 Pivotal Software, Inc. All rights reserved.
defmodule RabbitMQ.CLI.Diagnostics.Commands.LogTailCommand do
@moduledoc """
Displays standard log file location on the target node
"""
@behaviour RabbitMQ.CLI.CommandBehaviour
alias RabbitMQ.CLI.Core.LogFiles
def switches, do: [number: :integer]
def aliases, do: ['N': :number]
def merge_defaults(args, opts) do
{args, Map.merge(%{number: 10}, opts)}
end
use RabbitMQ.CLI.Core.AcceptsNoPositionalArguments
def run([], %{node: node_name, timeout: timeout, number: n}) do
case LogFiles.get_default_log_location(node_name, timeout) do
{:ok, file} ->
:rabbit_misc.rpc_call(node_name,
:rabbit_log_tail, :tail_n_lines, [file, n],
timeout)
error -> error
end
end
use RabbitMQ.CLI.DefaultOutput
def help_section(), do: :configuration
def description(), do: "Prints the last N lines of the log on the node"
def usage, do: "log_tail [--number|-N <number>]"
def usage_additional do
[
["<number>", "number of lines to print. 10 by default"]
]
end
def banner([], %{node: node_name, number: n}) do
"Last #{n} lines of the log on node #{node_name} ..."
end
end

View File

@ -510,7 +510,7 @@ defmodule RabbitMQCtl do
defp format_error({:error, :check_failed, err}, %{formatter: "json"}, _) do
{:error, ExitCodes.exit_unavailable(), err}
end
defp format_error({:error, :check_failed}, _, _) do
defp format_error({:error, :check_failed, _}, _, _) do
{:error, ExitCodes.exit_unavailable(), nil}
end