/
leo_logger_appender_file.erl
249 lines (216 loc) · 8.27 KB
/
leo_logger_appender_file.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
%%======================================================================
%%
%% Leo Logger
%%
%% Copyright (c) 2012-2017 Rakuten, Inc.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% @doc The file appender
%% @reference https://github.com/leo-project/leo_logger/blob/master/src/leo_logger_appender_file.erl
%% @end
%%======================================================================
-module(leo_logger_appender_file).
-author('Yosuke Hara').
-behaviour(leo_logger_behavior).
-include("leo_logger.hrl").
-include_lib("eunit/include/eunit.hrl").
-export([init/3, append/2, bulk_output/2,
sync/1, format/2, rotate/2,
close/1, filename/3]).
%%--------------------------------------------------------------------
%% API
%%--------------------------------------------------------------------
%% @doc Initialize the logger
%%
-spec(init(Appender, CallbackMod, Props) ->
{ok, #logger_state{}} | {error, _} when Appender::atom(),
CallbackMod::module(),
Props::[{atom(), any()}]).
init(Appender, CallbackMod, Props) ->
RootPath = leo_misc:get_value(?FILE_PROP_ROOT_PATH, Props),
FileName = leo_misc:get_value(?FILE_PROP_FILE_NAME, Props),
Level = leo_misc:get_value(?FILE_PROP_LOG_LEVEL, Props),
{{Y, M, D}, {H, _, _}} = calendar:now_to_local_time(os:timestamp()),
DateHour = {Y, M, D, H},
BasePath = filename:join(RootPath, FileName),
{ok, Curr} = file:get_cwd(),
BasePath1 = case BasePath of
"/" ++ _Rest -> BasePath;
"../" ++ _Rest -> BasePath;
"./" ++ Rest -> Curr ++ "/" ++ Rest;
_ -> Curr ++ "/" ++ BasePath
end,
BasePathLen = string:len(BasePath1),
BasePath2 = case (BasePathLen == string:rstr(BasePath1, "/")) of
true -> string:substr(BasePath1, 1, BasePathLen-1);
false -> BasePath1
end,
case catch open(BasePath2, DateHour) of
{'EXIT', Cause} ->
{error, Cause};
{CurrentFileName, Handler} ->
{ok, #logger_state{appender_type = Appender,
appender_mod = ?appender_mod(Appender),
callback_mod = CallbackMod,
props = [{?FILE_PROP_FILE_NAME, BasePath2},
{?FILE_PROP_CUR_NAME, CurrentFileName},
{?FILE_PROP_HANDLER, Handler}],
level = Level,
hourstamp = DateHour}}
end.
%% @doc Append a message to a file
%%
-spec(append(Msg, State) ->
#logger_state{} when Msg::#message_log{},
State::#logger_state{}).
append(#message_log{formatted_msg = FormattedMsg}, State) ->
Handler = leo_misc:get_value(?FILE_PROP_HANDLER, State#logger_state.props),
catch file:write(Handler, lists:flatten(FormattedMsg)),
State.
%% @doc Output messages
%%
-spec(bulk_output(Logs, State) ->
#logger_state{} when Logs::[_],
State::#logger_state{}).
bulk_output(_Logs, State) ->
State.
%% @doc Format a log message
%%
-spec(format(Type, Msg) ->
Ret when Type::split|bulk,
Msg::#message_log{},
Ret::string()).
format(_Type, #message_log{format = Format,
message = Message}) ->
case catch io_lib:format(Format, Message) of
{'EXIT', _Cause} ->
[];
Ret ->
Ret
end.
%% @doc Synchronize the file
%%
-spec(sync(State) ->
ok | {error, _} when State::#logger_state{}).
sync(State) ->
Handler = leo_misc:get_value(?FILE_PROP_HANDLER, State#logger_state.props),
file:datasync(Handler).
%% @doc Rotate the log file
%%
-spec(rotate(Hours, State) ->
{ok, #logger_state{}} when Hours::{integer(),
integer(),
integer(),
integer()},
State::#logger_state{}).
rotate(Hours, #logger_state{props = Props} = State) ->
BaseFileName = leo_misc:get_value(?FILE_PROP_FILE_NAME, Props),
CurrentFileName = leo_misc:get_value(?FILE_PROP_CUR_NAME, Props),
Handler = leo_misc:get_value(?FILE_PROP_HANDLER, Props),
ok = close(BaseFileName, CurrentFileName, Handler),
{NewLogFileName, NewHandler} = open(BaseFileName, Hours),
{ok, State#logger_state{props = [{?FILE_PROP_FILE_NAME, BaseFileName},
{?FILE_PROP_CUR_NAME, NewLogFileName},
{?FILE_PROP_HANDLER, NewHandler}],
hourstamp = Hours}}.
%% @doc Close the log file
-spec(close(State) ->
ok | {error, any()} when State::#logger_state{}).
close(#logger_state{props = Props} = _State) ->
LinkedFileName = leo_misc:get_value(?FILE_PROP_FILE_NAME, Props, []),
CurrentFileName = leo_misc:get_value(?FILE_PROP_CUR_NAME, Props, []),
Handler = leo_misc:get_value(?FILE_PROP_HANDLER, Props),
ok = close(LinkedFileName, CurrentFileName, Handler),
ok.
%% @doc Close a log file
%% @private
close(LinkedFileName, FileName, Handler) ->
io:format("* closing log file is ~s~n", [FileName]),
catch file:datasync(Handler),
catch file:close(Handler),
case filelib:file_size(FileName) of
0 ->
%% if the files size is zero, it is removed
catch file:delete(LinkedFileName),
catch file:delete(FileName);
_ ->
void
end,
ok.
%% @doc Create name of a new file
%% @private
filename(BaseFileName, DateHour, Branch) ->
FileName = lists:append([BaseFileName,
suffix(DateHour),
".", integer_to_list(Branch)
]),
case filelib:is_file(FileName) of
true ->
filename(BaseFileName, DateHour, Branch + 1);
_ ->
FileName
end.
%%--------------------------------------------------------------------
%%% INNER FUNCTIONS
%%--------------------------------------------------------------------
%% @doc Open a log file
%% @private
open(BaseFileName, DateHour) ->
_ = filelib:ensure_dir(BaseFileName),
FileName = filename(BaseFileName, DateHour, 1),
io:format("* opening log file is ~s~n", [FileName]),
{ok, Handler} = file:open(FileName, [read, write, raw]),
{ok, Location} = file:position(Handler, eof),
fix_log(Handler, Location),
case file:read_link(BaseFileName) of
{ok, _} ->
ok = file:delete(BaseFileName);
_ ->
void
end,
file:make_symlink(FileName, BaseFileName),
{FileName, Handler}.
%% @doc Seek backwards to the last valid log entry
%% @private
fix_log(_Handler, 0) ->
ok;
fix_log(Handler, 1) ->
{ok, 0} = file:position(Handler, 0),
ok;
fix_log(Handler, Location) ->
case file:pread(Handler, Location - 1, 1) of
{ok, [$\n | _]} ->
ok;
{ok, _} ->
fix_log(Handler, Location - 1)
end.
%% @doc Zero-padding number
%% @private
zeropad(Num, MinLength) ->
NumStr = integer_to_list(Num),
zeropad_str(NumStr, MinLength - length(NumStr)).
zeropad_str(NumStr, Zeros) when Zeros > 0 ->
zeropad_str([$0 | NumStr], Zeros - 1);
zeropad_str(NumStr, _) ->
NumStr.
%% @doc Create a suffix
%% @private
suffix({Y, M, D, H}) ->
YS = zeropad(Y, 4),
MS = zeropad(M, 2),
DS = zeropad(D, 2),
HS = zeropad(H, 2),
lists:flatten([$., YS, MS, DS, $., HS]).