ソースを参照

ft: 消息的format移动到写文件的函数里面

master
SisMaker 5ヶ月前
コミット
5088d0dedb
5個のファイルの変更42行の追加34行の削除
  1. +6
    -3
      include/lgDef.hrl
  2. +6
    -4
      src/backend/lgBkdFile.erl
  3. +1
    -22
      src/eLog.erl
  4. +24
    -2
      src/formatter/lgFmtTer.erl
  5. +5
    -3
      src/utils/lgUtil.erl

+ 6
- 3
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),

+ 6
- 4
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}}};

+ 1
- 22
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.
%%

+ 24
- 2
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);

+ 5
- 3
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),

読み込み中…
キャンセル
保存