From 0fb7824ca52d109d6077333bc585bc764d226ddb Mon Sep 17 00:00:00 2001 From: SisMaker <1713699517@qq.com> Date: Sat, 6 Mar 2021 22:54:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?ft:=20crashlog=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 ++-- eRum.sample.config | 2 +- src/backend/rumBackendFile.erl | 6 +++--- src/crashLog/rumCrashLog.erl | 16 ++++++++-------- src/eRum.erl | 2 +- src/utils/rumUtil.erl | 32 ++++++++++++++++---------------- 6 files changed, 31 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index e11b621..d23de31 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 0bf45be..e990a3c 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 8cf4535..1464d83 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 8f44d59..b30da8b 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 ad0ea75..2a98423 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,9 +479,9 @@ 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 +%% When logRoot option is provided, get the real path to a file +parsePath(RelPath) -> + RelPath2 = case rumUtil:get_env(logRoot, undefined) 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 @@ -491,7 +491,7 @@ expand_path(RelPath) -> false -> RelPath end; - undefined -> % No log_root given, keep relative path + undefined -> % No logRoot given, keep relative path RelPath end, %% see #534 make sure c:cd can't change file path, trans filename to abs name @@ -500,7 +500,7 @@ expand_path(RelPath) -> %% 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]) -> @@ -858,20 +858,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. From 043faaeae418c54b7adf390e1a3ba1e89aae3ecf Mon Sep 17 00:00:00 2001 From: SisMaker <1713699517@qq.com> Date: Mon, 8 Mar 2021 01:55:28 +0800 Subject: [PATCH 2/2] =?UTF-8?q?ft:=20crashlog=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/crashLog/rumCrashLog.erl | 2 +- src/utils/rumUtil.erl | 29 +++++++++++++---------------- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/crashLog/rumCrashLog.erl b/src/crashLog/rumCrashLog.erl index b30da8b..68c6ae5 100644 --- a/src/crashLog/rumCrashLog.erl +++ b/src/crashLog/rumCrashLog.erl @@ -187,7 +187,7 @@ do_log({log, Event}, #state{fileName = Name, fd = FD, inode = Inode, ctime = Cti case Rotator:ensure_logfile(Name, FD, Inode, Ctime, false) of {ok, {_FD, _Inode, _Ctime, Size}} when RotSize /= 0, Size > RotSize -> _ = Rotator:rotate_logfile(Name, Count), - handle_cast({log, Event}, State); + handleCast({log, Event}, State); {ok, {NewFD, NewInode, NewCtime, _Size}} -> TimeBinStr = rumUtil:msToBinStr(), Time = [TimeBinStr, " =", ReportStr, "====\n"], diff --git a/src/utils/rumUtil.erl b/src/utils/rumUtil.erl index 2a98423..c94ba6d 100644 --- a/src/utils/rumUtil.erl +++ b/src/utils/rumUtil.erl @@ -479,23 +479,20 @@ is_loggable(Msg, SeverityThreshold, MyName) when is_integer(SeverityThreshold) - rumMsg:severity_as_int(Msg) =< SeverityThreshold orelse lists:member(MyName, rumMsg:destinations(Msg)). -%% When logRoot option is provided, get the real path to a file parsePath(RelPath) -> - RelPath2 = case rumUtil:get_env(logRoot, undefined) 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 logRoot 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). + 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. %%