-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlog4m.m
205 lines (161 loc) · 5.95 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
classdef log4m < 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 <lawinslow@gmail.com>
% 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;
fid = 0;
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 setCommandWindowLevel(self,loggerIdentifier)
self.commandWindowLevel = loggerIdentifier;
end
function setLogLevel(self,logLevel)
self.logLevel = logLevel;
end
%% The public Logging methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function trace(self, message, varargin)
self.writeLog(self.TRACE,message,varargin);
end
function debug(self, message, varargin)
self.writeLog(self.DEBUG,message,varargin);
end
function info(self, message, varargin)
self.writeLog(self.INFO,message,varargin);
end
function warn(self, message, varargin)
self.writeLog(self.WARN,message,varargin);
end
function error(self, message, varargin)
self.writeLog(self.ERROR,message,varargin);
end
function fatal(self, message, varargin)
self.writeLog(self.FATAL,message,varargin);
end
end
%% Private Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Unless you're modifying this, these should be of little concern to you.
methods (Access = private)
function self = log4m(logPath)
self.fid = fopen(logPath, 'wt');
end
%% WriteToFile
function writeLog(self,level,message,args)
% If necessary write to command window
if( self.commandWindowLevel <= level )
fprintf('%s\n', sprintf(message, args{:}));
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
msg = sprintf(message, args{:});
now_str = datestr(now,'yyyy-mm-dd HH:MM:SS.FFF');
fprintf(self.fid, '%s %s\n', [now_str, ' ', levelStr, ' - '], msg);
catch ME_1
display(ME_1);
end
end
end
end