Skip to content

Commit 9a28686

Browse files
committed
Use OTP logger instead of lager.
1 parent 0cc2e02 commit 9a28686

13 files changed

+89
-107
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,12 @@ To change default settings, change `mtproto_proxy` section of `prod-sys.config`
253253
]}
254254
]},
255255

256-
{lager,
256+
{kernel,
257+
[{logger_level, info},
258+
{logger,
259+
[{handler, default, logger_std_h,
260+
#{config => #{file => "/var/log/mtproto-proxy/application.log"}}}
261+
]}]},
257262
<...>
258263
```
259264
(so, remove `%%`s) and replace `port` / `secret` / `tag` with yours.
@@ -281,7 +286,7 @@ To do so, just add more configs to `ports` section, separated by comma, eg:
281286
]}
282287
]},
283288

284-
{lager,
289+
{kernel,
285290
<...>
286291
```
287292

@@ -453,7 +458,7 @@ different `listen_ip` (one v4 and one v6):
453458
]}
454459
]},
455460
456-
{lager,
461+
{kernel,
457462
<...>
458463
```
459464

config/sys.config.example

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,19 @@
1616
]},
1717

1818
%% Logging config
19-
{lager,
20-
[{log_root, "/var/log/mtproto-proxy"},
21-
{crash_log, "crash.log"},
22-
{handlers,
23-
[
24-
{lager_console_backend,
25-
[{level, critical}]},
26-
27-
{lager_file_backend,
28-
[{file, "application.log"},
29-
{level, info},
30-
31-
%% Do fsync only on critical messages
32-
{sync_on, critical},
33-
%% If we logged more than X messages in a second, flush the rest
34-
{high_water_mark, 300},
35-
%% If we hit hwm and msg queue len is >X, flush the queue
36-
{flush_queue, true},
37-
{flush_threshold, 2000},
38-
%% How often to check if log should be rotated
39-
{check_interval, 5000},
40-
%% Rotate when file size is 100MB+
41-
{size, 104857600}
42-
]}
19+
{kernel,
20+
[{logger_level, info},
21+
{logger,
22+
[{handler, default, logger_std_h,
23+
#{level => info,
24+
config => #{type => file,
25+
file => "/var/log/mtproto-proxy/application.log",
26+
max_no_bytes => 104857600, % 100MB
27+
max_no_files => 10,
28+
filesync_repeat_interval => no_repeat}}},
29+
{handler, console, logger_std_h,
30+
#{level => critical,
31+
config => #{type => standard_io}}}
4332
]}]},
4433
{sasl,
4534
[{errlog_type, error}]}

rebar.config

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
% -*- mode: erlang -*-
2-
{erl_opts, [debug_info,
3-
{d, 'HUT_LAGER'},
4-
{parse_transform, lager_transform}]}.
2+
{erl_opts, [debug_info]}.
53

64
{deps, [{ranch, "1.7.0"},
7-
{hut, "1.3.0"},
8-
{lager, "3.9.1"},
95
{erlang_psq, {git, "https://github.com/seriyps/psq", {branch, "master"}}}
106
]}.
117
{project_plugins, [rebar3_proper,
@@ -22,8 +18,7 @@
2218
]}.
2319

2420
{relx, [{release, { mtp_proxy, "0.1.0" },
25-
[lager,
26-
ranch,
21+
[ranch,
2722
mtproto_proxy,
2823
sasl]},
2924

src/mtp_config.erl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
-type netloc() :: {inet:ip4_address(), inet:port_number()}.
3333
-type netloc_v4v6() :: {inet:ip_address(), inet:port_number()}.
3434

35-
-include_lib("hut/include/hut.hrl").
35+
-include_lib("kernel/include/logger.hrl").
3636

3737
-define(TAB, ?MODULE).
3838
-define(IPS_KEY(DcId), {id, DcId}).
@@ -145,7 +145,7 @@ handle_call(_Request, _From, State) ->
145145

146146
handle_cast(update, #state{timer = Timer} = State) ->
147147
update(State, soft),
148-
?log(info, "Config updated"),
148+
?LOG_INFO("Config updated"),
149149
Timer1 = gen_timeout:bump(
150150
gen_timeout:reset(Timer)),
151151
{noreply, State#state{timer = Timer1}}.
@@ -154,7 +154,7 @@ handle_info(timeout, #state{timer = Timer} =State) ->
154154
case gen_timeout:is_expired(Timer) of
155155
true ->
156156
update(State, soft),
157-
?log(info, "Config updated"),
157+
?LOG_INFO("Config updated"),
158158
Timer1 = gen_timeout:bump(
159159
gen_timeout:reset(Timer)),
160160
{noreply, State#state{timer = Timer1}};
@@ -177,8 +177,8 @@ update(#state{tab = Tab}, force) ->
177177
update(State, _) ->
178178
try update(State, force)
179179
catch Class:Reason:Stack ->
180-
?log(error, "Err updating proxy settings: ~s",
181-
[lager:pr_stacktrace(Stack, {Class, Reason})]) %XXX lager-specific
180+
?LOG_ERROR("Err updating proxy settings: ~s",
181+
[erl_error:format_exception(Class, Reason, Stack)])
182182
end.
183183

184184
update_key(Tab) ->
@@ -249,8 +249,8 @@ update_ip([Url | Fallbacks]) ->
249249
{ok, _} = inet:parse_ipv4strict_address(IpStr), %assert
250250
application:set_env(?APP, external_ip, IpStr)
251251
catch Class:Reason:Stack ->
252-
?log(error, "Failed to update IP with ~s service: ~s",
253-
[Url, lager:pr_stacktrace(Stack, {Class, Reason})]), %XXX - lager-specific
252+
?LOG_ERROR("Failed to update IP with ~s service: ~s",
253+
[Url, erl_error:format_exception(Class, Reason, Stack)]),
254254
update_ip(Fallbacks)
255255
end;
256256
update_ip([]) ->

src/mtp_dc_pool.erl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
terminate/2, code_change/3]).
2929
-export_type([status/0]).
3030

31-
-include_lib("hut/include/hut.hrl").
31+
-include_lib("kernel/include/logger.hrl").
3232

3333
-define(SERVER, ?MODULE).
3434
-define(APP, mtproto_proxy).
@@ -194,13 +194,13 @@ handle_down(MonRef, Pid, Reason, #state{downstreams = Ds,
194194
{Pid, DsM1} ->
195195
Pending1 = lists:delete(Pid, Pending),
196196
Ds1 = ds_remove(Pid, Ds),
197-
?log(error, "Downstream=~p is down. reason=~p", [Pid, Reason]),
197+
?LOG_ERROR("Downstream=~p is down. reason=~p", [Pid, Reason]),
198198
maybe_restart_connection(
199199
St#state{pending_downstreams = Pending1,
200200
downstreams = Ds1,
201201
downstream_monitors = DsM1});
202202
_ ->
203-
?log(error, "Unexpected DOWN. ref=~p, pid=~p, reason=~p", [MonRef, Pid, Reason]),
203+
?LOG_ERROR("Unexpected DOWN. ref=~p, pid=~p, reason=~p", [MonRef, Pid, Reason]),
204204
St
205205
end
206206
end.
@@ -321,7 +321,7 @@ ds_return(Pid, St) ->
321321
{ok, St1} ->
322322
St1;
323323
undefined ->
324-
?log(warning, "Attempt to release unknown connection ~p", [Pid]),
324+
?LOG_WARNING("Attempt to release unknown connection ~p", [Pid]),
325325
St
326326
end.
327327

src/mtp_down_conn.erl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
terminate/2, code_change/3]).
2828
-export_type([handle/0, upstream_opts/0]).
2929

30-
-include_lib("hut/include/hut.hrl").
30+
-include_lib("kernel/include/logger.hrl").
3131

3232
-define(SERVER, ?MODULE).
3333
-define(APP, mtproto_proxy).
@@ -129,12 +129,12 @@ handle_call({set_config, Name, Value}, _From, State) ->
129129
{{ok, State#state.backpressure_conf},
130130
State#state{backpressure_conf = BpConfig}}
131131
catch Type:Reason ->
132-
?log(error, "~p: not updating downstream_backpressure: ~p",
132+
?LOG_ERROR("~p: not updating downstream_backpressure: ~p",
133133
[Type, Reason]),
134134
{ignored, State}
135135
end;
136136
_ ->
137-
?log(warning, "set_config ~p=~p ignored", [Name, Value]),
137+
?LOG_WARNING("set_config ~p=~p ignored", [Name, Value]),
138138
{ignored, State}
139139
end,
140140
{reply, Response, State1}.
@@ -164,8 +164,8 @@ handle_info(do_connect, #state{dc_id = DcId} = State) ->
164164
{ok, St1} = connect(DcId, State),
165165
{noreply, St1}
166166
catch Class:Reason:Stack ->
167-
?log(error, "Down connect to dc=~w error: ~s",
168-
[DcId, lager:pr_stacktrace(Stack, {Class, Reason})]), %XXX lager-specific
167+
?LOG_ERROR("Down connect to dc=~w error: ~s",
168+
[DcId, erl_error:format_exception(Class, Reason, Stack)]),
169169
erlang:send_after(300, self(), do_connect),
170170
{noreply, State}
171171
end;
@@ -182,7 +182,7 @@ handle_info(handshake_timeout, #state{stage = Stage, dc_id = DcId} = St) ->
182182

183183
terminate(_Reason, #state{upstreams = Ups}) ->
184184
%% Should I do this or dc_pool? Maybe only when reason is 'normal'?
185-
?log(warning, "Downstream terminates with reason ~p; len(upstreams)=~p",
185+
?LOG_WARNING("Downstream terminates with reason ~p; len(upstreams)=~p",
186186
[_Reason, map_size(Ups)]),
187187
Self = self(),
188188
lists:foreach(
@@ -201,7 +201,7 @@ handle_send(Data, Upstream, #state{upstreams = Ups,
201201
Packet = mtp_rpc:encode_packet({data, Data}, {UpstreamStatic, ProxyAddr}),
202202
down_send(Packet, St);
203203
_ ->
204-
?log(warning, "Upstream=~p not found", [Upstream]),
204+
?LOG_WARNING("Upstream=~p not found", [Upstream]),
205205
{{error, unknown_upstream}, St}
206206
end.
207207

@@ -214,7 +214,7 @@ handle_upstream_new(Upstream, Opts, #state{upstreams = Ups,
214214
UpsStatic = {ConnId, iolist_to_binary(mtp_rpc:encode_ip_port(Ip, Port)), AdTag},
215215
Ups1 = Ups#{Upstream => {UpsStatic, 0, 0}},
216216
UpsRev1 = UpsRev#{ConnId => Upstream},
217-
?log(debug, "New upstream=~p conn_id=~p", [Upstream, ConnId]),
217+
?LOG_DEBUG("New upstream=~p conn_id=~p", [Upstream, ConnId]),
218218
St#state{upstreams = Ups1,
219219
upstreams_rev = UpsRev1}.
220220

@@ -232,7 +232,7 @@ handle_upstream_closed(Upstream, #state{upstreams = Ups,
232232
down_send(Packet, St2);
233233
error ->
234234
%% It happens when we get rpc_close_ext
235-
?log(info, "Unknown upstream ~p", [Upstream]),
235+
?LOG_INFO("Unknown upstream ~p", [Upstream]),
236236
{ok, St}
237237
end.
238238

@@ -282,13 +282,13 @@ handle_rpc({close_ext, ConnId}, St) ->
282282
St2#state{upstreams = Ups1,
283283
upstreams_rev = UpsRev1};
284284
error ->
285-
?log(warning, "Unknown upstream ~p", [ConnId]),
285+
?LOG_WARNING("Unknown upstream ~p", [ConnId]),
286286
St1
287287
end;
288288
handle_rpc({simple_ack, ConnId, Confirm}, S) ->
289289
up_send({simple_ack, self(), Confirm}, ConnId, S);
290290
handle_rpc({unknown, Tag, Tail}, S) ->
291-
?log(info, "Unknown packet from backend. Tag ~w, tail: ~w", [Tag, Tail]),
291+
?LOG_INFO("Unknown packet from backend. Tag ~w, tail: ~w", [Tag, Tail]),
292292
S.
293293

294294

@@ -318,7 +318,7 @@ up_send(Packet, ConnId, #state{upstreams_rev = UpsRev} = St) ->
318318
St
319319
end;
320320
error ->
321-
?log(warning, "Unknown connection_id=~w", [ConnId]),
321+
?LOG_WARNING("Unknown connection_id=~w", [ConnId]),
322322
%% WHY!!!?
323323
%% ClosedPacket = mtp_rpc:encode_packet(remote_closed, ConnId),
324324
%% {ok, St1} = down_send(ClosedPacket, St),
@@ -460,7 +460,7 @@ connect(DcId, S) ->
460460
mtp_metric:count_inc([?APP, out_connect_ok, total], 1,
461461
#{labels => [DcId]}),
462462
AddrStr = inet:ntoa(Host),
463-
?log(info, "~s:~p: TCP connected", [AddrStr, Port]),
463+
?LOG_INFO("~s:~p: TCP connected", [AddrStr, Port]),
464464
down_handshake1(S#state{sock = Sock,
465465
netloc = {Host, Port}});
466466
{error, Reason} = Err ->
@@ -558,7 +558,7 @@ down_handshake3(Pkt, #state{stage_state = {Deadline, PrevSenderPid}, pool = Pool
558558
{handshake, _SenderPid, PeerPid} = mtp_rpc:decode_handshake(Pkt),
559559
(PeerPid == PrevSenderPid) orelse error({wrong_sender_pid, PeerPid}),
560560
ok = mtp_dc_pool:ack_connected(Pool, self()),
561-
?log(info, "~s:~w: dc=~w handshake complete", [inet:ntoa(Addr), Port, DcId]),
561+
?LOG_INFO("~s:~w: dc=~w handshake complete", [inet:ntoa(Addr), Port, DcId]),
562562
{ok, S#state{stage = tunnel,
563563
stage_state = undefined}}.
564564

src/mtp_fake_tls.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
-export_type([codec/0, meta/0]).
3030

31-
-include_lib("hut/include/hut.hrl").
31+
-include_lib("kernel/include/logger.hrl").
3232

3333
-dialyzer(no_improper_lists).
3434

@@ -108,7 +108,7 @@ from_client_hello(Data, Secret) ->
108108
#client_hello{pseudorandom = ClientDigest,
109109
session_id = SessionId,
110110
extensions = Extensions} = CliHlo = parse_client_hello(Data),
111-
?log(debug, "TLS ClientHello=~p", [CliHlo]),
111+
?LOG_DEBUG("TLS ClientHello=~p", [CliHlo]),
112112
ServerDigest = make_server_digest(Data, Secret),
113113
<<Zeroes:(?DIGEST_LEN - 4)/binary, Timestamp:32/unsigned-little>> = XoredDigest =
114114
crypto:exor(ClientDigest, ServerDigest),

0 commit comments

Comments
 (0)