Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 120 lines (91 sloc) 3.197 kb
d9342f8 @capflam server-specific logger_mod and auth_mod (capflam)
capflam authored
1 %%%----------------------------------------------------------------------
2 %%% File : yaws_logger.erl
3 %%% Author : Christopher Faulet <christopher@yakaz.com>
4 %%% Purpose :
5 %%% Created : 14 Dec 2010 by Christopher Faulet <christopher@yakaz.com>
6 %%%----------------------------------------------------------------------
7
8 -module(yaws_logger).
9 -author('christopher@yakaz.com').
10 -include_lib("kernel/include/file.hrl").
11
12
13 -export([behaviour_info/1]).
14
15 %% API
16 -export([
17 open_log/3,
18 close_log/2,
19 close_logs/0,
20 rotate/1,
21
22 accesslog/6,
23 authlog/4
24 ]).
25
26
27 -include("../include/yaws.hrl").
28 -include("../include/yaws_api.hrl").
29 -include("yaws_debug.hrl").
30
31
32 -record(log, {id, amod, data}).
33
34 %%%----------------------------------------------------------------------
35 %%% API
36 %%%----------------------------------------------------------------------
37 behaviour_info(callbacks) ->
38 [{open_log,3}, {close_log,3}, {wrap_log,4}, {write_log,4}];
39 behaviour_info(_Other) ->
40 undefined.
41
42
43 open_log(SConf, auth, Dir) when ?sc_has_auth_log(SConf) ->
44 do_open_log(SConf, auth, Dir);
45 open_log(SConf, access, Dir) when ?sc_has_access_log(SConf) ->
46 do_open_log(SConf, access, Dir);
47 open_log(_, _, _) ->
48 false.
49
50
51 close_log(SConf, Type) ->
52 case ets:lookup(yaws_log, {Type, SConf#sconf.servername}) of
53 [AL] ->
54 do_close_log(AL),
55 ets:delete(yaws_log, {Type, SConf#sconf.servername}),
56 ok;
57 [] ->
58 ok
59 end.
60
61
62 close_logs() ->
63 do_close_logs(ets:first(yaws_log)),
64 ets:delete_all_objects(yaws_log),
65 ok.
66
67
68 rotate(LogWrapSize) ->
69 do_rotate(ets:first(yaws_log), LogWrapSize).
70
71
72 accesslog(#sconf{servername=Srv}, Ip, Req, InH, OutH, Time) ->
73 case ets:lookup(yaws_log, {access, Srv}) of
74 [#log{amod=Mod, data=Data}] ->
75 catch Mod:write_log(Srv, access, Data, {Ip, Req, InH, OutH, Time});
76 _ ->
77 ok
78 end.
79
80 authlog(#sconf{servername=Srv}, IP, Path, Item) ->
81 case ets:lookup(yaws_log, {auth, Srv}) of
82 [#log{amod=Mod, data=Data}] ->
83 catch Mod:write_log(Srv, auth, Data, {IP, Path, Item});
84 _ ->
85 ok
86 end.
87
88 %%%----------------------------------------------------------------------
89 %%% Internal functions
90 %%%----------------------------------------------------------------------
91 do_open_log(#sconf{servername=Srv, logger_mod=Mod}, Type, Dir) ->
92 case catch Mod:open_log(Srv, Type, Dir) of
93 {true, Data} ->
94 AL = #log{id={Type, Srv}, amod=Mod, data=Data},
95 ets:insert(yaws_log, AL),
96 true;
97 _ ->
98 false
99 end.
100
101
102 do_close_log(#log{id={Type, Srv}, amod=Mod, data=Data}) ->
103 catch Mod:close_log(Srv, Type, Data).
104
105
106 do_close_logs('$end_of_table') ->
107 ok;
108 do_close_logs(Id) ->
109 [AL] = ets:lookup(yaws_log, Id),
110 do_close_log(AL),
111 do_close_logs(ets:next(yaws_log, Id)).
112
113
114 do_rotate('$end_of_table', _) ->
115 ok;
116 do_rotate(Id, LogWrapSize) ->
117 [#log{id={Type, Srv}, amod=Mod, data=Data}=AL] = ets:lookup(yaws_log, Id),
118 Data1 = Mod:wrap_log(Srv, Type, Data, LogWrapSize),
119 ets:insert(yaws_log, AL#log{data=Data1}).
Something went wrong with that request. Please try again.