-
Notifications
You must be signed in to change notification settings - Fork 2
/
log4m.m
270 lines (222 loc) · 8.58 KB
/
log4m.m
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
classdef log4m < matlab.mixin.Copyable %handle
%LOG4M This is a simple logger based on the idea of the popular log4j.
%
% Description: Log4m is designed to be relatively fast and very easy to
% use. It has been designed to work well in a matlab environment.
% Please contact me (info below) with any questions or suggestions!
%
%
% Author:
% Luke Winslow <[email protected]>
% Heavily modified version of 'log4matlab' which can be found here:
% http://www.mathworks.com/matlabcentral/fileexchange/33532-log4matlab
%
properties (Constant)
ALL = 0;
TRACE = 1;
DEBUG = 2;
INFO = 3;
WARN = 4;
ERROR = 5;
FATAL = 6;
OFF = 7;
end
properties(Access = protected)
logger;
lFile;
end
properties(SetAccess = protected)
fullpath = 'log4m.log'; %Default file
commandWindowLevel = log4m.ALL;
logLevel = log4m.INFO;
end
methods (Static)
function obj = getLogger( logPath )
%GETLOGGER Returns instance unique logger object.
% PARAMS:
% logPath - Relative or absolute path to desired logfile.
% OUTPUT:
% obj - Reference to signular logger object.
%
if(nargin == 0)
logPath = 'log4m.log';
elseif(nargin > 1)
error('getLogger only accepts one parameter input');
end
persistent localObj;
if isempty(localObj) || ~isvalid(localObj)
localObj = log4m(logPath);
end
obj = localObj;
end
function testSpeed( logPath )
%TESTSPEED Gives a brief idea of the time required to log.
%
% Description: One major concern with logging is the
% performance hit an application takes when heavy logging is
% introduced. This function does a quick speed test to give
% the user an idea of how various types of logging will
% perform on their system.
%
L = log4m.getLogger(logPath);
disp('1e5 logs when logging only to command window');
L.setCommandWindowLevel(L.TRACE);
L.setLogLevel(L.OFF);
tic;
for i=1:1e5
L.trace('log4mTest','test');
end
disp('1e5 logs when logging only to command window');
toc;
disp('1e6 logs when logging is off');
L.setCommandWindowLevel(L.OFF);
L.setLogLevel(L.OFF);
tic;
for i=1:1e6
L.trace('log4mTest','test');
end
toc;
disp('1e4 logs when logging to file');
L.setCommandWindowLevel(L.OFF);
L.setLogLevel(L.TRACE);
tic;
for i=1:1e4
L.trace('log4mTest','test');
end
toc;
end
end
%% Public Methods Section %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
methods
function setFilename(self,logPath)
%SETFILENAME Change the location of the text log file.
%
% PARAMETERS:
% logPath - Name or full path of desired logfile
%
[fid,message] = fopen(logPath, 'a');
if(fid < 0)
error(['Problem with supplied logfile path: ' message]);
end
fclose(fid);
self.fullpath = logPath;
end
function setCommandWindowLevel(self,loggerIdentifier)
self.commandWindowLevel = loggerIdentifier;
end
function setLogLevel(self,logLevel)
self.logLevel = logLevel;
end
%% The public Logging methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function trace(self, funcName, message)
%TRACE Log a message with the TRACE level
%
% PARAMETERS:
% funcName - Name of the function or location from which
% message is coming.
% message - Text of message to log.
%
self.writeLog(self.TRACE,funcName,message);
end
function debug(self, funcName, message)
%TRACE Log a message with the DEBUG level
%
% PARAMETERS:
% funcName - Name of the function or location from which
% message is coming.
% message - Text of message to log.
%
self.writeLog(self.DEBUG,funcName,message);
end
function info(self, funcName, message)
%TRACE Log a message with the INFO level
%
% PARAMETERS:
% funcName - Name of the function or location from which
% message is coming.
% message - Text of message to log.
%
self.writeLog(self.INFO,funcName,message);
end
function warn(self, funcName, message)
%TRACE Log a message with the WARN level
%
% PARAMETERS:
% funcName - Name of the function or location from which
% message is coming.
% message - Text of message to log.
%
self.writeLog(self.WARN,funcName,message);
end
function error(self, funcName, message)
%TRACE Log a message with the ERROR level
%
% PARAMETERS:
% funcName - Name of the function or location from which
% message is coming.
% message - Text of message to log.
%
self.writeLog(self.ERROR,funcName,message);
end
function fatal(self, funcName, message)
%TRACE Log a message with the FATAL level
%
% PARAMETERS:
% funcName - Name of the function or location from which
% message is coming.
% message - Text of message to log.
%
self.writeLog(self.FATAL,funcName,message);
end
end
%% Private Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Unless you're modifying this, these should be of little concern to you.
methods (Access = private)
function self = log4m(fullpath_passed)
if(nargin > 0)
path = fullpath_passed;
end
self.setFilename(path);
end
%% WriteToFile
function writeLog(self,level,scriptName,message)
% If necessary write to command window
if( self.commandWindowLevel <= level )
fprintf('%s:%s\n', scriptName, message);
end
%If currently set log level is too high, just skip this log
if(self.logLevel > level)
return;
end
% set up our level string
switch level
case{self.TRACE}
levelStr = 'TRACE';
case{self.DEBUG}
levelStr = 'DEBUG';
case{self.INFO}
levelStr = 'INFO';
case{self.WARN}
levelStr = 'WARN';
case{self.ERROR}
levelStr = 'ERROR';
case{self.FATAL}
levelStr = 'FATAL';
otherwise
levelStr = 'UNKNOWN';
end
% Append new log to log file
try
fid = fopen(self.fullpath,'a');
fprintf(fid,'%s %s %s - %s\r\n' ...
, datestr(now,'yyyy-mm-dd HH:MM:SS,FFF') ...
, levelStr ...
, scriptName ... % Have left this one with the '.' if it is passed
, message);
fclose(fid);
catch ME_1
display(ME_1);
end
end
end
end