浏览代码

ft: Def Rotator模块修改

master
SisMaker 4 年前
父节点
当前提交
d23092d66d
共有 6 个文件被更改,包括 41 次插入44 次删除
  1. +2
    -2
      README.md
  2. +1
    -1
      eRum.sample.config
  3. +3
    -3
      src/backend/rumBackendFile.erl
  4. +8
    -8
      src/crashLog/rumCrashLog.erl
  5. +1
    -1
      src/eRum.erl
  6. +26
    -29
      src/utils/rumUtil.erl

+ 2
- 2
README.md 查看文件

@ -56,7 +56,7 @@ eRum:warning("Some message with a term: ~p", [Term])
```erlang ```erlang
{eRum, [ {eRum, [
%% log_root是可选的,默认情况下文件路径是相对于CWD的 %% log_root是可选的,默认情况下文件路径是相对于CWD的
{log_root, "/var/log/hello"},
{logRoot, "/var/log/hello"},
{handlers, [ {handlers, [
{lager_console_backend, [{level, info}]}, {lager_console_backend, [{level, info}]},
{lager_file_backend, [{file, "error.log"}, {level, error}]}, {lager_file_backend, [{file, "error.log"}, {level, error}]},
@ -101,7 +101,7 @@ below). If async values are not configured, no overload protection will be appli
```erlang ```erlang
[{eRum, [ [{eRum, [
{log_root, "/tmp"},
{logRoot, "/tmp"},
%% Default handlers for lager/lager_event %% Default handlers for lager/lager_event
{handlers, [ {handlers, [
{lager_console_backend, [{level, info}]}, {lager_console_backend, [{level, info}]},

+ 1
- 1
eRum.sample.config 查看文件

@ -38,7 +38,7 @@
%% ********************************************** 日志文件配置相关 ************************************************ %% ********************************************** 日志文件配置相关 ************************************************
%% 可选的日志路径, 默认情况下是当前路径 %% 可选的日志路径, 默认情况下是当前路径
{log_root, "./log"},
{logRoot, "./log"},
%% crash log cfg %% crash log cfg
%% 有效值 string | false %% 有效值 string | false

+ 3
- 3
src/backend/rumBackendFile.erl 查看文件

@ -98,7 +98,7 @@ init(LogFileConfig) when is_list(LogFileConfig) ->
[RelName, Level, Date, Size, Count, Rotator, HighWaterMark, Flush, SyncInterval, SyncSize, SyncOn, CheckInterval, Formatter, FormatterConfig] = [RelName, Level, Date, Size, Count, Rotator, HighWaterMark, Flush, SyncInterval, SyncSize, SyncOn, CheckInterval, Formatter, FormatterConfig] =
[proplists:get_value(Key, Config) || Key <- [file, level, date, size, count, rotator, high_water_mark, flush_queue, sync_interval, sync_size, sync_on, check_interval, formatter, formatter_config]], [proplists:get_value(Key, Config) || Key <- [file, level, date, size, count, rotator, high_water_mark, flush_queue, sync_interval, sync_size, sync_on, check_interval, formatter, formatter_config]],
FlushThr = proplists:get_value(flush_threshold, Config, 0), FlushThr = proplists:get_value(flush_threshold, Config, 0),
Name = rumUtil:expand_path(RelName),
Name = rumUtil:parsePath(RelName),
schedule_rotation(Name, Date), schedule_rotation(Name, Date),
Shaper = rumUtil:maybe_flush(Flush, #rumShaper{hwm = HighWaterMark, flushThreshold = FlushThr, id = Name}), Shaper = rumUtil:maybe_flush(Flush, #rumShaper{hwm = HighWaterMark, flushThreshold = FlushThr, id = Name}),
State0 = #state{name = Name, level = Level, size = Size, date = Date, count = Count, rotator = Rotator, State0 = #state{name = Name, level = Level, size = Size, date = Date, count = Count, rotator = Rotator,
@ -950,13 +950,13 @@ filesystem_test_() ->
LogName = "foo.log", LogName = "foo.log",
LogPath = filename:join(TestDir, LogName), LogPath = filename:join(TestDir, LogName),
application:set_env(lager, log_root, TestDir),
application:set_env(lager, logRoot, TestDir),
{ok, _} = eRum:trace_file(LogName, [{module, ?MODULE}]), {ok, _} = eRum:trace_file(LogName, [{module, ?MODULE}]),
eRum:error("Test message"), eRum:error("Test message"),
%% not eligible for trace %% not eligible for trace
eRum:log(error, self(), "Test message"), eRum:log(error, self(), "Test message"),
{ok, Bin3} = file:read_file(LogPath), {ok, Bin3} = file:read_file(LogPath),
application:unset_env(lager, log_root),
application:unset_env(lager, logRoot),
?assertMatch([_, _, "[error]", _, "Test message\n"], ?assertMatch([_, _, "[error]", _, "Test message\n"],
re:split(Bin3, " ", [{return, list}, {parts, 5}])) re:split(Bin3, " ", [{return, list}, {parts, 5}]))
end}, end},

+ 8
- 8
src/crashLog/rumCrashLog.erl 查看文件

@ -36,13 +36,13 @@
fileName :: string(), %% fileName :: string(), %%
fd :: pid() | undefined, %% fd :: pid() | undefined, %%
inode :: integer() | undefined, inode :: integer() | undefined,
ctime :: file:date_time() | undefined,
fmtmaxbytes :: integer(),
size :: integer(),
date :: undefined | string(),
count :: integer(),
flap = false :: boolean(),
rotator :: atom()
ctime :: file:date_time() | undefined, %%
fmtmaxbytes :: integer(), %%
size :: integer(), %%
date :: undefined | string(), %%
count :: integer(), %%
flap = false :: boolean(), %%
rotator :: atom() %%
}). }).
start(Filename, MaxBytes, Size, Date, Count, Rotator) -> start(Filename, MaxBytes, Size, Date, Count, Rotator) ->
@ -54,7 +54,7 @@ start_link(Filename, MaxBytes, Size, Date, Count, Rotator) ->
% File, MsgMaxBytes, RotationSize, RotationDate, RotationCount, RotationMod % File, MsgMaxBytes, RotationSize, RotationDate, RotationCount, RotationMod
init({RelFilename, MaxBytes, Size, Date, Count, Rotator}) -> init({RelFilename, MaxBytes, Size, Date, Count, Rotator}) ->
Filename = rumUtil:expand_path(RelFilename),
Filename = rumUtil:parsePath(RelFilename),
case Rotator:openLogFile(Filename, false) of case Rotator:openLogFile(Filename, false) of
{ok, {FD, Inode, Ctime, _Size}} -> {ok, {FD, Inode, Ctime, _Size}} ->
schedule_rotation(Date), schedule_rotation(Date),

+ 1
- 1
src/eRum.erl 查看文件

@ -259,7 +259,7 @@ trace_file(File, Filter, Options) when is_list(Options) ->
trace_file(File, Filter, debug, Options). trace_file(File, Filter, debug, Options).
trace_file(File, Filter, Level, Options) -> trace_file(File, Filter, Level, Options) ->
FileName = rumUtil:expand_path(File),
FileName = rumUtil:parsePath(File),
case validate_trace_filters(Filter, Level, {lager_file_backend, FileName}) of case validate_trace_filters(Filter, Level, {lager_file_backend, FileName}) of
{Sink, {ok, Trace}} -> {Sink, {ok, Trace}} ->
Handlers = rumConfig:global_get(handlers, []), Handlers = rumConfig:global_get(handlers, []),

+ 26
- 29
src/utils/rumUtil.erl 查看文件

@ -21,7 +21,7 @@
, is_loggable/3 , is_loggable/3
, trace_filter/1 , trace_filter/1
, trace_filter/2 , trace_filter/2
, expand_path/1
, parsePath/1
, find_file/2 , find_file/2
, check_hwm/1 , check_hwm/1
, check_hwm/2 , check_hwm/2
@ -479,28 +479,25 @@ is_loggable(Msg, SeverityThreshold, MyName) when is_integer(SeverityThreshold) -
rumMsg:severity_as_int(Msg) =< SeverityThreshold orelse rumMsg:severity_as_int(Msg) =< SeverityThreshold orelse
lists:member(MyName, rumMsg:destinations(Msg)). lists:member(MyName, rumMsg:destinations(Msg)).
%% When log_root option is provided, get the real path to a file
expand_path(RelPath) ->
RelPath2 = case application:get_env(lager, log_root) of
{ok, LogRoot} when is_list(LogRoot) -> % Join relative path
%% check if the given RelPath contains LogRoot, if so, do not add
%% it again; see gh #304
case filename:dirname(RelPath) of
"." ->
filename:join(LogRoot, RelPath);
false ->
RelPath
end;
undefined -> % No log_root given, keep relative path
RelPath
end,
%% see #534 make sure c:cd can't change file path, trans filename to abs name
filename:absname(RelPath2).
parsePath(RelPath) ->
NewRelPath =
case rumUtil:get_env(logRoot, undefined) of
undefined ->
RelPath;
LogRoot ->
case filename:dirname(RelPath) of
"." ->
filename:join(LogRoot, RelPath);
false ->
RelPath
end
end,
filename:absname(NewRelPath).
%% Find a file among the already installed handlers. %% Find a file among the already installed handlers.
%% %%
%% The file is already expanded (i.e. lager_util:expand_path already added the %% The file is already expanded (i.e. lager_util:expand_path already added the
%% "log_root"), but the file paths inside Handlers are not.
%% "logRoot"), but the file paths inside Handlers are not.
find_file(_File1, _Handlers = []) -> find_file(_File1, _Handlers = []) ->
false; false;
find_file(File1, [{{lager_file_backend, File2}, _Handler, _Sink} = HandlerInfo | Handlers]) -> find_file(File1, [{{lager_file_backend, File2}, _Handler, _Sink} = HandlerInfo | Handlers]) ->
@ -853,20 +850,20 @@ mask_to_levels_test() ->
ok. ok.
expand_path_test() -> expand_path_test() ->
OldRootVal = application:get_env(lager, log_root),
OldRootVal = application:get_env(lager, logRoot),
ok = application:unset_env(lager, log_root),
?assertEqual(filename:absname("/foo/bar"), expand_path("/foo/bar")),
?assertEqual(filename:absname("foo/bar"), expand_path("foo/bar")),
ok = application:unset_env(lager, logRoot),
?assertEqual(filename:absname("/foo/bar"), parsePath("/foo/bar")),
?assertEqual(filename:absname("foo/bar"), parsePath("foo/bar")),
ok = application:set_env(lager, log_root, "log/dir"),
?assertEqual(filename:absname("/foo/bar"), expand_path("/foo/bar")), % Absolute path should not be changed
?assertEqual(filename:absname("log/dir/foo/bar"), expand_path("foo/bar")),
?assertEqual(filename:absname("log/dir/foo/bar"), expand_path("log/dir/foo/bar")), %% gh #304
ok = application:set_env(lager, logRoot, "log/dir"),
?assertEqual(filename:absname("/foo/bar"), parsePath("/foo/bar")), % Absolute path should not be changed
?assertEqual(filename:absname("log/dir/foo/bar"), parsePath("foo/bar")),
?assertEqual(filename:absname("log/dir/foo/bar"), parsePath("log/dir/foo/bar")), %% gh #304
case OldRootVal of case OldRootVal of
undefined -> application:unset_env(lager, log_root);
{ok, Root} -> application:set_env(lager, log_root, Root)
undefined -> application:unset_env(lager, logRoot);
{ok, Root} -> application:set_env(lager, logRoot, Root)
end, end,
ok. ok.

正在加载...
取消
保存