-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogger.dyalog
187 lines (162 loc) · 5.8 KB
/
Logger.dyalog
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
:Class Logger ⍝ Logs HTTP requests
⎕IO←⎕ML←1
⍝ /Config/Logger.xml fields (and defaults) are:
⍝ <active>0</active> <!-- 1 for yes, 0 for no -->
⍝ <anonymousIPs>1</anonymousIPs> <!-- 1 for yes (GDPR compliant), 0 for no -->
⍝ <directory>%SiteRoot%/Logs</directory> <!-- directory to store log files -->
⍝ <interval>10</interval> <!-- cache write time interval in seconds, 0 means write immediately (do not cache) -->
⍝ <prefix></prefix> <!-- character vector log file prefix -->
:field public instance Active←0
:field private instance anonymousIps←1
:field private instance directory←''
:field private instance interval←10
:field private instance prefix←''
:field private instance badFolder←0
:field server
:field private instance TieNo←⍬
:field private instance Cache←''
:field private instance tid←¯1
missing←{0∊⍴⍵:'-' ⋄ ⍵}
Char←⎕DR ' '
serverLog←{⍺←1 ⋄ ⍺ server.Log 'Logger: ',⍵}
∇ Make ms;config;msg
:Access public
:Implements Constructor
EOL←⎕UCS 13 10↓⍨~#.DUI.isWin
server←ms
:If 0≢config←ConfigureLogger ms
Active←config.active
Anonymize←{3='.'+.=⍵:((-(⌽⍵)⍳'.')↓⍵),'.0' ⋄ ⍵}⍣config.anonymousIps
Prefix←config.prefix
Interval←config.interval
Directory←config.directory
:If ~#.Files.DirExists Directory
msg←'Log file directory "',Directory,'" '
:If config.createLogFolder
:Trap 0
:If 2 #.Files.MkDir Directory
msg,←' created'
:Else
msg,←' was not able to be created.'
badFolder←1
:EndIf
:Else
msg,←' encountered a ',(⎕DMX.EM),' during creation.'
badFolder←1
:EndTrap
:Else
msg,←' does not exist.'
badFolder←1
:EndIf
serverLog msg
:EndIf
:If badFolder<Active
Start
:ElseIf ~Active
4 serverLog'logging initialized, but is not active per configuration setting'
:EndIf
:EndIf
∇
∇ UnMake
:Implements destructor
Stop ⍬
∇
∇ Stop w
:Access public
:Trap 0
:If Active
Active←0
⎕TKILL tid
:If Open
ClearCache
Close
:EndIf
serverLog'logging stopped'
:EndIf
:EndTrap
∇
∇ Start
:Access public
:If ~tid∊⎕TNUMS
Active←1
tid←Run&0
serverLog'logging started to folder ',Directory
:EndIf
∇
∇ Run x;done
:While Active
{}⎕DL Interval
:If Open
ClearCache
Close
:EndIf
:EndWhile
∇
∇ r←Open;fn
⎕NUNTIE TieNo
TieNo←0
fn←#.Files.Normalize Directory,Prefix,(⍕100⊥3↑⎕TS),'.log'
:Trap 22 ⍝ file name error
TieNo←fn ⎕NTIE 0
:Else
:Trap 0
TieNo←{0 ⎕NTIE⍨⍵⊣⎕NUNTIE ⍵ ⎕NCREATE 0}fn
4 serverLog'log file "',fn,'" created'
:Else
server.Log'Unable to open log file "',fn,'"'
:Return
:EndTrap
:EndTrap
r←0≠TieNo
∇
tryGetting←{0::('-'@(' '∘=))⎕DMX.EM,'-retrieving-"',(∊⍕⍵),'"' ⋄ ∊⍕⍎⍵}
∇ Log req;addr;user;ts;method;page;status;msec;bytes
:Access public
:If Active
:Hold 'Lumberjack'
:Trap 0
Cache,←((missing Anonymize 2⊃req.PeerAddr),' ',(missing req.Session.User),#.Dates.LogFmtNow,'"',req.Method,' ',req.Page,'"',∊' '∘,∘⍕¨req.Response.(Status MSec Bytes)),EOL
:Else ⍝ something in logging failed, try figuring out what
addr←tryGetting'req.PeerAddr'
user←tryGetting'req.Session.User'
ts←tryGetting'#.Dates.LogFmtNow'
method←tryGetting'req.Method'
page←tryGetting'req.Page'
status←tryGetting'req.Response.Status'
msec←tryGetting'req.Response.MSec'
bytes←tryGetting'req.Response.Bytes'
Cache,←(missing Anonymize addr),' ',(missing user),ts,'"',method,' ',page,'" ',status,' ',msec,' ',bytes,EOL
:EndTrap
:EndHold
:EndIf
∇
∇ ClearCache
:Hold 'Lumberjack'
:If Char≠⎕DR Cache
Cache←('?'@{Char≠⎕DR¨⍵})Cache
:EndIf
Cache ⎕NAPPEND TieNo
Cache←''
:EndHold
∇
∇ Close
⎕NUNTIE TieNo
∇
∇ config←ConfigureLogger ms;file;log;config;Setting
⍝ load logger information
config←0
:If 0∊⍴log←#.DUI.ReadConfiguration'Logger'
1 ms.Log'No Logger configuration file was found'
:Else
Setting←#.DUI.Setting
config←ms.Config.LoggerConfig←⎕NS''
ms.Config.LoggerConfig.active←0
config.active←log Setting'active' 1 0
config.anonymousIps←log Setting'anonymousIps' 1 1
config.createLogFolder←log Setting'createLogFolder' 1 0
config.directory←#.DUI.{folderize SubstPath ⍵}log Setting'directory' 0 'logs'
config.interval←log Setting'interval' 1 10 ⍝
config.prefix←log Setting'prefix' 0 ''
:EndIf
∇
:EndClass