From 5088d0dedb21a1dbce7a7c5cf00348acf9d91e71 Mon Sep 17 00:00:00 2001 From: SisMaker <1713699517@qq.com> Date: Fri, 29 Nov 2024 17:33:16 +0800 Subject: [PATCH] =?UTF-8?q?ft:=20=E6=B6=88=E6=81=AF=E7=9A=84format?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E5=88=B0=E5=86=99=E6=96=87=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E9=87=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/lgDef.hrl | 9 ++++++--- src/backend/lgBkdFile.erl | 10 ++++++---- src/eLog.erl | 23 +---------------------- src/formatter/lgFmtTer.erl | 26 ++++++++++++++++++++++++-- src/utils/lgUtil.erl | 8 +++++--- 5 files changed, 42 insertions(+), 34 deletions(-) diff --git a/include/lgDef.hrl b/include/lgDef.hrl index b0b7f81..92fed68 100644 --- a/include/lgDef.hrl +++ b/include/lgDef.hrl @@ -101,8 +101,11 @@ , line :: integer() , metadata :: [tuple()] , timestamp :: non_neg_integer() - , message :: list() - , destinations :: list() + , msgFormat :: list() + , msgArgs :: list() + , msgSafety = unsafe :: atom() + , msgFormatSize = 0 :: integer() + % , destinations :: list() }). -type lgShaper() :: #lgShaper{}. @@ -120,7 +123,7 @@ -define(LgShouldLog(Level), ?eLogCfg:get(?LgDefSink) band Level =/= 0). -define(LgNotify(Level, Pid, Format, Args), - gen_emm:info_notify(?LgDefSink, {mWriteLog, #lgMsg{severity = Level, pid = Pid, module = ?MODULE, function = ?FUNCTION_NAME, line = ?LINE, metadata = [], timestamp = lgTime:nowMs(), message = eFmt:format(Format, Args), destinations = []}})). + gen_emm:info_notify(?LgDefSink, {mWriteLog, #lgMsg{severity = Level, pid = Pid, module = ?MODULE, function = ?FUNCTION_NAME, line = ?LINE, metadata = [], timestamp = lgTime:nowMs(), msgFormat = Format, msgArgs = Args}})). %%仅供内部使用仅内部非阻塞日志记录调用,当我们仍在启动大型啤酒时尝试进行日志记录(通常为错误)时,会有一些特殊处理。 -define(INT_LOG(Level, Format, Args), diff --git a/src/backend/lgBkdFile.erl b/src/backend/lgBkdFile.erl index 5a61235..aeea005 100644 --- a/src/backend/lgBkdFile.erl +++ b/src/backend/lgBkdFile.erl @@ -136,9 +136,10 @@ handleEvent({mWriteLog, Message}, #state{fBName = _FBName, level = Level, shaper true -> State; _ -> - ReportStr = eFmt:format(<<"lgBkdFile dropped ~p messages in the last second that exceeded the limit of ~p messages/sec">>, [Drop, NewShaper#lgShaper.hwm]), + Format = <<"lgBkdFile dropped ~p messages in the last second that exceeded the limit of ~p messages/sec">>, + Args = [Drop, NewShaper#lgShaper.hwm], NowMs = lgTime:nowMs(), - ReportMsg = #lgMsg{severity = ?llvWarning, pid = self(), module = ?MODULE, function = ?FUNCTION_NAME, line = ?LINE, metadata = [], timestamp = NowMs, message = ReportStr, destinations = []}, + ReportMsg = #lgMsg{severity = ?llvWarning, pid = self(), module = ?MODULE, function = ?FUNCTION_NAME, line = ?LINE, metadata = [], timestamp = NowMs, msgFormat = Format, msgArgs = Args}, writeLog(State, NowMs, ?llvWarning, FmtTer:format(ReportMsg, FmtCfg)) end, {noreply, writeLog(TemState#state{shaper = NewShaper}, Timestamp, Severity, FmtTer:format(Message, FmtCfg))}; @@ -163,9 +164,10 @@ handleInfo({mShaperExpired, FBName}, #state{shaper = Shaper, fBName = FBName, fm 0 -> ignore; Dropped -> - ReportStr = eFmt:format(<<"lgBkdFile dropped ~p messages in the last second that exceeded the limit of ~p messages/sec">>, [Dropped, Shaper#lgShaper.hwm]), + Format = <<"lgBkdFile dropped ~p messages in the last second that exceeded the limit of ~p messages/sec">>, + Args = [Dropped, Shaper#lgShaper.hwm], NowMs = lgTime:nowMs(), - ReportMsg = #lgMsg{severity = ?llvWarning, pid = self(), module = ?MODULE, function = ?FUNCTION_NAME, line = ?LINE, metadata = [], timestamp = NowMs, message = ReportStr, destinations = []}, + ReportMsg = #lgMsg{severity = ?llvWarning, pid = self(), module = ?MODULE, function = ?FUNCTION_NAME, line = ?LINE, metadata = [], timestamp = NowMs, msgFormat = Format, msgArgs = Args}, writeLog(State, NowMs, ?llvWarning, FmtTer:format(ReportMsg, FmtCfg)) end, {noreply, State#state{shaper = Shaper#lgShaper{dropped = 0}}}; diff --git a/src/eLog.erl b/src/eLog.erl index 0103dd0..eec84d8 100644 --- a/src/eLog.erl +++ b/src/eLog.erl @@ -15,8 +15,6 @@ %% log and log param , dispatchLog/11 , doLogImpl/11 - , safeFormat/3 - , unsafeFormat/2 , getMd/0 , setMd/1 , getMdPd/0 @@ -82,9 +80,8 @@ dispatchLog(Sink, Severity, Pid, Module, Function, Line, Metadata, Format, Args, doLogImpl(Severity, Pid, Module, Function, Line, Metadata, Format, Args, Size, Sink, Safety) -> % TraceFilters = lgConfig:ptGet({Sink, trace}, []), %% 这个功能还没用上 % Destinations = [], % %% 这个功能还没用上 ?lgCASE(TraceFilters =/= [], lgUtil:check_traces(Metadata, Severity, TraceFilters, []), []), - MsgStr = ?lgCASE(Args =/= [] andalso Args =/= undefined, ?lgCASE(Safety == safe, safeFormat(Format, Args, [{charsLimit, Size}]), unsafeFormat(Format, Args)), Format), NowMs = lgTime:nowMs(), - LgMsg = #lgMsg{severity = Severity, pid = Pid, module = Module, function = Function, line = Line, metadata = Metadata, timestamp = NowMs, message = MsgStr, destinations = []}, + LgMsg = #lgMsg{severity = Severity, pid = Pid, module = Module, function = Function, line = Line, metadata = Metadata, timestamp = NowMs, msgFormat = Format, msgArgs = Args, msgSafety = Safety, msgFormatSize = Size}, case lgConfig:ptGet({Sink, async}, true) of true -> gen_emm:info_notify(Sink, {mWriteLog, LgMsg}); @@ -488,24 +485,6 @@ add_trace_to_loglevel_config(Trace, Sink) -> ok end. -%% @doc Print the format string `Fmt' with `Args' safely with a size -%% limit of `Limit'. If the format string is invalid, or not enough -%% arguments are supplied 'FORMAT ERROR' is printed with the offending -%% arguments. The caller is NOT crashed. - -unsafeFormat(Fmt, Args) -> - try eFmt:format(Fmt, Args) - catch - _:_ -> eFmt:format(<<"FORMAT ERROR SAFE: ~p ~p">>, [Fmt, Args]) - end. - -safeFormat(Fmt, Args, Limit) -> - try eFmt:format(Fmt, Args, Limit) - catch - _:_ -> - eFmt:format(<<"FORMAT ERROR UNSAFE: ~p ~p">>, [Fmt, Args], Limit) - end. - %% @private Print the format string `Fmt' with `Args' without a size limit. %% This is unsafe because the output of this function is unbounded. %% diff --git a/src/formatter/lgFmtTer.erl b/src/formatter/lgFmtTer.erl index 480e1a3..02a66c1 100644 --- a/src/formatter/lgFmtTer.erl +++ b/src/formatter/lgFmtTer.erl @@ -64,12 +64,32 @@ fmtCfg(MetaWhitelist) -> [datetime, sev, node, <<"|">>, pid, <<"|">>, module, <<"|">>, function, <<"|">>, line, <<"|">>] ++ [{M, [atom_to_binary(M), <<"=">>, M, "|"], [<<>>]} || M <- MetaWhitelist] ++ [message, <<"\n">>]. +%% @doc Print the format string `Fmt' with `Args' safely with a size +%% limit of `Limit'. If the format string is invalid, or not enough +%% arguments are supplied 'FORMAT ERROR' is printed with the offending +%% arguments. The caller is NOT crashed. + +unsafeFormat(Fmt, Args) -> + try eFmt:formatIol(Fmt, Args) + catch + _:_ -> eFmt:formatIol(<<"FORMAT ERROR SAFE: ~p ~p">>, [Fmt, Args]) + end. + +safeFormat(Fmt, Args, Limit) -> + try eFmt:formatIol(Fmt, Args, Limit) + catch + _:_ -> + eFmt:formatIol(<<"FORMAT ERROR UNSAFE: ~p ~p">>, [Fmt, Args], Limit) + end. + % Level, Pid, Node, Module, Function, FunctionArity, Line -define(FixMd, [pid, node, module, function, line]). -spec output(term(), lgMsg()) -> iolist(). -output(message, LgMsg) -> LgMsg#lgMsg.message; +output(message, LgMsg) -> + #lgMsg{msgFormat = Format, msgArgs = Args, msgSafety = Safety, msgFormatSize = Size} = LgMsg, + ?lgCASE(Args =/= [] andalso Args =/= undefined, ?lgCASE(Safety == safe, safeFormat(Format, Args, [{charsLimit, Size}]), unsafeFormat(Format, Args)), Format); output(datetime, LgMsg) -> lgUtil:msToBinStr(LgMsg#lgMsg.timestamp); output(pid, LgMsg) -> pid_to_list(LgMsg#lgMsg.pid); output(node, _LgMsg) -> ?eLogCfg:get(?eLogNodeName); @@ -110,7 +130,9 @@ output({Prop, Present, Absent, Width}, LgMsg) when is_atom(Prop) -> end; output(Other, _) -> makeStr(Other). -output(message, LgMsg, _Width) -> LgMsg#lgMsg.message; +output(message, LgMsg, _Width) -> + #lgMsg{msgFormat = Format, msgArgs = Args, msgSafety = Safety, msgFormatSize = Size} = LgMsg, + ?lgCASE(Args =/= [] andalso Args =/= undefined, ?lgCASE(Safety == safe, safeFormat(Format, Args, [{charsLimit, Size}]), unsafeFormat(Format, Args)), Format); output(datetime, LgMsg, _Width) -> lgUtil:msToBinStr(LgMsg#lgMsg.timestamp); output(pid, LgMsg, _Width) -> pid_to_list(LgMsg#lgMsg.pid); output(node, _LgMsg, _Width) -> ?eLogCfg:get(?eLogNodeName); diff --git a/src/utils/lgUtil.erl b/src/utils/lgUtil.erl index 932b9e5..b873d27 100644 --- a/src/utils/lgUtil.erl +++ b/src/utils/lgUtil.erl @@ -500,9 +500,11 @@ isErrorReport(crash_report) -> true; isErrorReport(_) -> false. -spec isLoggAble(lgMsg(), lgMaskLevel(), term()) -> boolean(). -isLoggAble(LgMsg, Mask, MyName) -> - #lgMsg{severity = Severity, destinations = Destinations} = LgMsg, - (Severity band Mask) =/= 0 orelse lists:member(MyName, Destinations). +isLoggAble(LgMsg, Mask, _MyName) -> + %#lgMsg{severity = Severity, destinations = Destinations} = LgMsg, + %(Severity band Mask) =/= 0 orelse lists:member(MyName, Destinations). + #lgMsg{severity = Severity} = LgMsg, + Severity band Mask =/= 0. parsePath(FBName) -> LogRoot = lgUtil:get_env(logRoot, ?LgDefLogRoot),