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
%% Copyright (c) 2011 Basho Technologies, Inc. All Rights Reserved.
%%
%% 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 lager logging framework.
-module(lager).
-include("lager.hrl").
%% API
-export([start/0,
log/7, log/8, log/3, log/4,
get_loglevel/1, set_loglevel/2, set_loglevel/3, get_loglevels/0,
minimum_loglevel/1, posix_error/1]).
-type log_level() :: debug | info | notice | warning | error | critical | alert | emergency.
-type log_level_number() :: 0..7.
-export_type([log_level/0, log_level_number/0]).
%% API
%% @doc Start the application. Mainly useful for using `-s lager' as a command
%% line switch to the VM to make lager start on boot.
start() -> start(lager).
start(App) ->
start_ok(App, application:start(App, permanent)).
start_ok(_App, ok) -> ok;
start_ok(_App, {error, {already_started, _App}}) -> ok;
start_ok(App, {error, {not_started, Dep}}) ->
ok = start(Dep),
start(App);
start_ok(App, {error, Reason}) ->
erlang:error({app_start_failed, App, Reason}).
%% @private
-spec log(log_level(), atom(), atom(), pos_integer(), pid(), tuple(), list()) ->
ok | {error, lager_not_running}.
log(Level, Module, Function, Line, Pid, Time, Message) ->
Timestamp = lager_util:format_time(Time),
Msg = [io_lib:format("[~p] ~p@~p:~p:~p ", [Level, Pid, Module,
Function, Line]), string:strip(lists:flatten(Message), right, $\n)],
safe_notify(lager_util:level_to_num(Level), Timestamp, Msg).
%% @private
-spec log(log_level(), atom(), atom(), pos_integer(), pid(), tuple(), string(), list()) ->
ok | {error, lager_not_running}.
log(Level, Module, Function, Line, Pid, Time, Format, Args) ->
Timestamp = lager_util:format_time(Time),
Msg = [io_lib:format("[~p] ~p@~p:~p:~p ", [Level, Pid, Module,
Function, Line]), string:strip(lists:flatten(io_lib:format(Format, Args)), right, $\n)],
safe_notify(lager_util:level_to_num(Level), Timestamp, Msg).
%% @doc Manually log a message into lager without using the parse transform.
-spec log(log_level(), pid(), list()) -> ok | {error, lager_not_running}.
log(Level, Pid, Message) ->
Timestamp = lager_util:format_time(),
Msg = [io_lib:format("[~p] ~p ", [Level, Pid]), string:strip(lists:flatten(Message), right, $\n)],
safe_notify(lager_util:level_to_num(Level), Timestamp, Msg).
%% @doc Manually log a message into lager without using the parse transform.
-spec log(log_level(), pid(), string(), list()) -> ok | {error, lager_not_running}.
log(Level, Pid, Format, Args) ->
Timestamp = lager_util:format_time(),
Msg = [io_lib:format("[~p] ~p ", [Level, Pid]), string:strip(lists:flatten(io_lib:format(Format, Args)), right, $\n)],
safe_notify(lager_util:level_to_num(Level), Timestamp, Msg).
%% @doc Set the loglevel for a particular backend.
set_loglevel(Handler, Level) when is_atom(Level) ->
Reply = gen_event:call(lager_event, Handler, {set_loglevel, Level}, infinity),
%% recalculate min log level
MinLog = minimum_loglevel(get_loglevels()),
lager_mochiglobal:put(loglevel, MinLog),
Reply.
%% @doc Set the loglevel for a particular backend that has multiple identifiers
%% (eg. the file backend).
set_loglevel(Handler, Ident, Level) when is_atom(Level) ->
Reply = gen_event:call(lager_event, Handler, {set_loglevel, Ident, Level}, infinity),
%% recalculate min log level
MinLog = minimum_loglevel(get_loglevels()),
lager_mochiglobal:put(loglevel, MinLog),
Reply.
%% @doc Get the loglevel for a particular backend. In the case that the backend
%% has multiple identifiers, the lowest is returned
get_loglevel(Handler) ->
case gen_event:call(lager_event, Handler, get_loglevel, infinity) of
X when is_integer(X) ->
lager_util:num_to_level(X);
Y -> Y
end.
%% @doc Try to convert an atom to a posix error, but fall back on printing the
%% term if its not a valid posix error code.
posix_error(Error) when is_atom(Error) ->
case erl_posix_msg:message(Error) of
"unknown POSIX error" -> atom_to_list(Error);
Message -> Message
end;
posix_error(Error) ->
lists:flatten(io_lib:format("~p", [Error])).
%% @private
get_loglevels() ->
[gen_event:call(lager_event, Handler, get_loglevel, infinity) ||
Handler <- gen_event:which_handlers(lager_event)].
%% @private
minimum_loglevel([]) ->
-1; %% lower than any log level, logging off
minimum_loglevel(Levels) ->
erlang:hd(lists:reverse(lists:sort(Levels))).
safe_notify(Level, Timestamp, Msg) ->
case whereis(lager_event) of
undefined ->
%% lager isn't running
{error, lager_not_running};
Pid ->
gen_event:sync_notify(Pid, {log, Level, Timestamp, Msg})
end.