瀏覽代碼

Merge pull request #241 from stolen/log_root

Add optional log_root environment variable
pull/247/merge
Jon Meredith 10 年之前
父節點
當前提交
b7984d4628
共有 4 個檔案被更改,包括 34 行新增3 行删除
  1. +3
    -0
      README.md
  2. +2
    -1
      src/lager_crash_log.erl
  3. +2
    -1
      src/lager_file_backend.erl
  4. +27
    -1
      src/lager_util.erl

+ 3
- 0
README.md 查看文件

@ -77,6 +77,7 @@ your app.config):
```erlang ```erlang
{lager, [ {lager, [
{log_root, "/var/log/hello"},
{handlers, [ {handlers, [
{lager_console_backend, info}, {lager_console_backend, info},
{lager_file_backend, [{file, "error.log"}, {level, error}]}, {lager_file_backend, [{file, "error.log"}, {level, error}]},
@ -85,6 +86,8 @@ your app.config):
]}. ]}.
``` ```
```log_root``` variable is optional, by default file paths are relative to CWD.
The available configuration options for each backend are listed in their The available configuration options for each backend are listed in their
module's documentation. module's documentation.

+ 2
- 1
src/lager_crash_log.erl 查看文件

@ -66,7 +66,8 @@ start(Filename, MaxBytes, Size, Date, Count) ->
Date, Count], []). Date, Count], []).
%% @private %% @private
init([Filename, MaxBytes, Size, Date, Count]) ->
init([RelFilename, MaxBytes, Size, Date, Count]) ->
Filename = lager_util:expand_path(RelFilename),
case lager_util:open_logfile(Filename, false) of case lager_util:open_logfile(Filename, false) of
{ok, {FD, Inode, _}} -> {ok, {FD, Inode, _}} ->
schedule_rotation(Date), schedule_rotation(Date),

+ 2
- 1
src/lager_file_backend.erl 查看文件

@ -102,8 +102,9 @@ init(LogFileConfig) when is_list(LogFileConfig) ->
{error, {fatal, bad_config}}; {error, {fatal, bad_config}};
Config -> Config ->
%% probabably a better way to do this, but whatever %% probabably a better way to do this, but whatever
[Name, Level, Date, Size, Count, SyncInterval, SyncSize, SyncOn, CheckInterval, Formatter, FormatterConfig] =
[RelName, Level, Date, Size, Count, SyncInterval, SyncSize, SyncOn, CheckInterval, Formatter, FormatterConfig] =
[proplists:get_value(Key, Config) || Key <- [file, level, date, size, count, sync_interval, sync_size, sync_on, check_interval, formatter, formatter_config]], [proplists:get_value(Key, Config) || Key <- [file, level, date, size, count, sync_interval, sync_size, sync_on, check_interval, formatter, formatter_config]],
Name = lager_util:expand_path(RelName),
schedule_rotation(Name, Date), schedule_rotation(Name, Date),
State0 = #state{name=Name, level=Level, size=Size, date=Date, count=Count, formatter=Formatter, State0 = #state{name=Name, level=Level, size=Size, date=Date, count=Count, formatter=Formatter,
formatter_config=FormatterConfig, sync_on=SyncOn, sync_interval=SyncInterval, sync_size=SyncSize, formatter_config=FormatterConfig, sync_on=SyncOn, sync_interval=SyncInterval, sync_size=SyncSize,

+ 27
- 1
src/lager_util.erl 查看文件

@ -22,7 +22,7 @@
open_logfile/2, ensure_logfile/4, rotate_logfile/2, format_time/0, format_time/1, open_logfile/2, ensure_logfile/4, rotate_logfile/2, format_time/0, format_time/1,
localtime_ms/0, localtime_ms/1, maybe_utc/1, parse_rotation_date_spec/1, localtime_ms/0, localtime_ms/1, maybe_utc/1, parse_rotation_date_spec/1,
calculate_next_rotation/1, validate_trace/1, check_traces/4, is_loggable/3, calculate_next_rotation/1, validate_trace/1, check_traces/4, is_loggable/3,
trace_filter/1, trace_filter/2]).
trace_filter/1, trace_filter/2, expand_path/1]).
-ifdef(TEST). -ifdef(TEST).
-include_lib("eunit/include/eunit.hrl"). -include_lib("eunit/include/eunit.hrl").
@ -467,6 +467,15 @@ i2l(I) -> integer_to_list(I).
i3l(I) when I < 100 -> [$0 | i2l(I)]; i3l(I) when I < 100 -> [$0 | i2l(I)];
i3l(I) -> integer_to_list(I). i3l(I) -> integer_to_list(I).
%% When log_root option is provided, get the real path to a file
expand_path(RelPath) ->
case application:get_env(lager, log_root) of
{ok, LogRoot} when is_list(LogRoot) -> % Join relative path
filename:join(LogRoot, RelPath);
undefined -> % No log_root given, keep relative path
RelPath
end.
-ifdef(TEST). -ifdef(TEST).
parse_test() -> parse_test() ->
@ -707,4 +716,21 @@ mask_to_levels_test() ->
?assertEqual([debug, notice, error], mask_to_levels(?DEBUG bor ?NOTICE bor ?ERROR)), ?assertEqual([debug, notice, error], mask_to_levels(?DEBUG bor ?NOTICE bor ?ERROR)),
ok. ok.
expand_path_test() ->
OldRootVal = application:get_env(lager, log_root),
ok = application:unset_env(lager, log_root),
?assertEqual("/foo/bar", expand_path("/foo/bar")),
?assertEqual("foo/bar", expand_path("foo/bar")),
ok = application:set_env(lager, log_root, "log/dir"),
?assertEqual("/foo/bar", expand_path("/foo/bar")), % Absolute path should not be changed
?assertEqual("log/dir/foo/bar", expand_path("foo/bar")),
case OldRootVal of
undefined -> application:unset_env(lager, log_root);
{ok, Root} -> application:set_env(lager, log_root, Root)
end,
ok.
-endif. -endif.

Loading…
取消
儲存