diff --git a/README.md b/README.md index c31182c..c51301a 100644 --- a/README.md +++ b/README.md @@ -56,7 +56,7 @@ eRum:warning("Some message with a term: ~p", [Term]) ```erlang {eRum, [ %% log_root是可选的,默认情况下文件路径是相对于CWD的 - {log_root, "/var/log/hello"}, + {logRoot, "/var/log/hello"}, {handlers, [ {lager_console_backend, [{level, info}]}, {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 [{eRum, [ - {log_root, "/tmp"}, + {logRoot, "/tmp"}, %% Default handlers for lager/lager_event {handlers, [ {lager_console_backend, [{level, info}]}, diff --git a/eRum.sample.config b/eRum.sample.config index 2608741..4860621 100644 --- a/eRum.sample.config +++ b/eRum.sample.config @@ -38,7 +38,7 @@ %% ********************************************** 日志文件配置相关 ************************************************ %% 可选的日志路径, 默认情况下是当前路径 - {log_root, "./log"}, + {logRoot, "./log"}, %% crash log cfg %% 有效值 string | false diff --git a/src/backend/rumBackendFile.erl b/src/backend/rumBackendFile.erl index 33da295..2dfa8a1 100644 --- a/src/backend/rumBackendFile.erl +++ b/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] = [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), - Name = rumUtil:expand_path(RelName), + Name = rumUtil:parsePath(RelName), schedule_rotation(Name, Date), 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, @@ -950,13 +950,13 @@ filesystem_test_() -> LogName = "foo.log", 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}]), eRum:error("Test message"), %% not eligible for trace eRum:log(error, self(), "Test message"), {ok, Bin3} = file:read_file(LogPath), - application:unset_env(lager, log_root), + application:unset_env(lager, logRoot), ?assertMatch([_, _, "[error]", _, "Test message\n"], re:split(Bin3, " ", [{return, list}, {parts, 5}])) end}, diff --git a/src/crashLog/rumCrashLog.erl b/src/crashLog/rumCrashLog.erl index 5bcbb43..214388c 100644 --- a/src/crashLog/rumCrashLog.erl +++ b/src/crashLog/rumCrashLog.erl @@ -36,13 +36,13 @@ fileName :: string(), %% 文件名 fd :: pid() | 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) -> @@ -54,7 +54,7 @@ start_link(Filename, MaxBytes, Size, Date, Count, Rotator) -> % File, MsgMaxBytes, RotationSize, RotationDate, RotationCount, RotationMod init({RelFilename, MaxBytes, Size, Date, Count, Rotator}) -> - Filename = rumUtil:expand_path(RelFilename), + Filename = rumUtil:parsePath(RelFilename), case Rotator:openLogFile(Filename, false) of {ok, {FD, Inode, Ctime, _Size}} -> schedule_rotation(Date), diff --git a/src/eRum.erl b/src/eRum.erl index 51f2dd3..610e835 100644 --- a/src/eRum.erl +++ b/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, Level, Options) -> - FileName = rumUtil:expand_path(File), + FileName = rumUtil:parsePath(File), case validate_trace_filters(Filter, Level, {lager_file_backend, FileName}) of {Sink, {ok, Trace}} -> Handlers = rumConfig:global_get(handlers, []), diff --git a/src/utils/rumUtil.erl b/src/utils/rumUtil.erl index c642650..fe36f14 100644 --- a/src/utils/rumUtil.erl +++ b/src/utils/rumUtil.erl @@ -21,7 +21,7 @@ , is_loggable/3 , trace_filter/1 , trace_filter/2 - , expand_path/1 + , parsePath/1 , find_file/2 , check_hwm/1 , check_hwm/2 @@ -479,28 +479,25 @@ is_loggable(Msg, SeverityThreshold, MyName) when is_integer(SeverityThreshold) - rumMsg:severity_as_int(Msg) =< SeverityThreshold orelse 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. %% %% 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 = []) -> false; find_file(File1, [{{lager_file_backend, File2}, _Handler, _Sink} = HandlerInfo | Handlers]) -> @@ -853,20 +850,20 @@ mask_to_levels_test() -> ok. 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 - 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, ok.