Sfoglia il codice sorgente

Crash log rotation & documentation on rotation

pull/1/head
Andrew Thompson 13 anni fa
parent
commit
6eb8240f1a
5 ha cambiato i file con 58 aggiunte e 16 eliminazioni
  1. +7
    -1
      README.org
  2. +7
    -1
      src/lager.app.src
  3. +24
    -9
      src/lager_crash_log.erl
  4. +7
    -2
      src/lager_file_backend.erl
  5. +13
    -3
      src/lager_sup.erl

+ 7
- 1
README.org Vedi File

@ -53,7 +53,10 @@
{lager,
{handlers, [
{lager_console_backend, info},
{lager_file_backend, [{"error.log", error}, {"console.log", info}]}
{lager_file_backend, [
{"error.log", error, 10485760, "", 5},
{"console.log", info, 10485760, "", 5}
]}
]}
}.
#+END_EXAMPLE
@ -72,6 +75,9 @@
information. The location of the crash log can be specified by the crash_log
application variable. If undefined it is not written at all.
Messages in the crash log are subject to a maximum message size which can be
specified via the crash_log_msg_size application variable.
* Runtime loglevel changes
You can change the log level of any lager backend at runtime by doing the
following:

+ 7
- 1
src/lager.app.src Vedi File

@ -23,7 +23,13 @@
%% Whether to write a crash log, and where. Undefined means no crash logger.
{crash_log, "log/crash.log"},
%% Maximum size in bytes of events in the crash log - defaults to 65536
{crash_log_size, 65536},
{crash_log_msg_size, 65536},
%% Maximum size of the crash log in bytes, before its rotated, set
%% to 0 to disable rotation - default is 0
{crash_log_size, 10485760},
%% Number of rotated crash logs to keep, 0 means keep only the
%% current one - default is 0
{crash_log_count, 5},
%% Whether to redirect error_logger messages into lager - defaults to true
{error_logger_redirect, true}
]}

+ 24
- 9
src/lager_crash_log.erl Vedi File

@ -16,9 +16,15 @@
%% @doc Lager crash log writer. This module implements a gen_server which writes
%% error_logger error messages out to a file in their original format. The
%% location to which it logs is configured by the environment var `crash_log'.
%% location to which it logs is configured by the application var `crash_log'.
%% Omitting this variable disables crash logging. Crash logs are printed safely
%% using trunc_io via code mostly lifted from riak_err.
%%
%% The `crash_log_msg_size' application var is used to specify the maximum
%% size of any message to be logged. `crash_log_size' is used to specify the
%% maximum size of the crash log before it will be rotated (0 will disable)
%% and to control the number of rotated files to be retained, use
%% `crash_log_count'.
-module(lager_crash_log).
@ -30,29 +36,34 @@
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
code_change/3]).
-export([start_link/2, start/2]).
-export([start_link/5, start/5]).
-record(state, {
name,
fd,
inode,
fmtmaxbytes,
size,
count,
flap=false
}).
%% @private
start_link(Filename, MaxBytes) ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [Filename, MaxBytes], []).
start_link(Filename, MaxBytes, Size, _Date, Count) ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [Filename, MaxBytes,
Size, Count], []).
%% @private
start(Filename, MaxBytes) ->
gen_server:start({local, ?MODULE}, ?MODULE, [Filename, MaxBytes], []).
start(Filename, MaxBytes, Size, _Date, Count) ->
gen_server:start({local, ?MODULE}, ?MODULE, [Filename, MaxBytes, Size,
Count], []).
%% @private
init([Filename, MaxBytes]) ->
init([Filename, MaxBytes, Size, Count]) ->
case lager_util:open_logfile(Filename, false) of
{ok, {FD, Inode, _}} ->
{ok, #state{name=Filename, fd=FD, inode=Inode, fmtmaxbytes=MaxBytes}};
{ok, #state{name=Filename, fd=FD, inode=Inode,
fmtmaxbytes=MaxBytes, size=Size, count=Count}};
Error ->
Error
end.
@ -62,7 +73,8 @@ handle_call(_Call, _From, State) ->
{reply, ok, State}.
%% @private
handle_cast({log, Event}, #state{name=Name, fd=FD, inode=Inode, flap=Flap, fmtmaxbytes=FmtMaxBytes} = State) ->
handle_cast({log, Event}, #state{name=Name, fd=FD, inode=Inode, flap=Flap,
fmtmaxbytes=FmtMaxBytes, size=RotSize, count=Count} = State) ->
%% borrowed from riak_err
{ReportStr, Pid, MsgStr, _ErrorP} = case Event of
{error, _GL, {Pid1, Fmt, Args}} ->
@ -78,6 +90,9 @@ handle_cast({log, Event}, #state{name=Name, fd=FD, inode=Inode, flap=Flap, fmtma
{noreply, State};
true ->
case lager_util:ensure_logfile(Name, FD, Inode, false) of
{ok, {_, _, Size}} when RotSize /= 0, Size > RotSize ->
lager_util:rotate_logfile(Name, Count),
handle_cast({log, Event}, State);
{ok, {NewFD, NewInode, _Size}} ->
{Date, TS} = lager_util:format_time(lager_stdlib:maybe_utc(erlang:localtime())),
Time = [Date, " ", TS," =", ReportStr, "====\n"],

+ 7
- 2
src/lager_file_backend.erl Vedi File

@ -17,8 +17,13 @@
%% @doc File backend for lager, with multiple file support.
%% Multiple files are supported, each with the path and the loglevel being
%% configurable. The configuration paramter for this backend is a list of
%% 2-tuples of the form `{FileName, Level}'. This backend supports external log
%% rotation and will re-open handles to files if the inode changes.
%% 5-tuples of the form
%% `{FileName, Level, RotationSize, RotationDate, RotationCount}'.
%% This backend supports external and internal log
%% rotation and will re-open handles to files if the inode changes. It will
%% also rotate the files itself if the size of the file exceeds the
%% `RotationSize' and keep `RotationCount' rotated files. `RotationDate' is
%% currently ignored.
-module(lager_file_backend).

+ 13
- 3
src/lager_sup.erl Vedi File

@ -41,11 +41,21 @@ init([]) ->
%% check if the crash log is enabled
Crash = case application:get_env(lager, crash_log) of
{ok, File} ->
MaxBytes = case application:get_env(lager, crash_log_size) of
{ok, Val} -> Val;
MaxBytes = case application:get_env(lager, crash_log_msg_size) of
{ok, Val} when is_integer(Val) -> Val;
_ -> 65536
end,
[{lager_crash_log, {lager_crash_log, start_link, [File, MaxBytes]},
RotationSize = case application:get_env(lager, crash_log_size) of
{ok, Val1} when is_integer(Val1) -> Val1;
_ -> 0
end,
RotationCount = case application:get_env(lager, crash_log_count) of
{ok, Val2} when is_integer(Val2) -> Val2;
_ -> 0
end,
[{lager_crash_log, {lager_crash_log, start_link, [File, MaxBytes,
RotationSize, "", RotationCount]},
permanent, 5000, worker, [lager_crash_log]}];
_ ->
[]

Caricamento…
Annulla
Salva