1 classdef Log4jConfigurator<handle
3 properties (Constant,Abstract)
5 CHILD_LOG_FILE_NAME_PREFIX
13 methods (Static,Access=
protected)
14 function res = getSetConfStatus(varargin)
15 persistent isConfiguredPersistent;
16 if isempty(isConfiguredPersistent)
17 isConfiguredPersistent = false;
20 isConfiguredPersistent = varargin{1};
22 res = isConfiguredPersistent;
24 function res = getSetLockStatus(varargin)
25 persistent isLockedPersistent;
26 if isempty(isLockedPersistent)
27 isLockedPersistent =
false;
30 isLockedPersistent = varargin{1};
32 res = isLockedPersistent;
34 function res = getSetLogPropStr(varargin)
36 persistent logPropStrPersistent;
38 if isempty(logPropStrPersistent)
39 logPropStrPersistent = char(logPropStrPersistent);
42 inpLogPropStr=varargin{1};
43 if ~ischar(inpLogPropStr)
45 'to be a character array']);
47 logPropStrPersistent = varargin{1};
49 res=logPropStrPersistent;
53 function res = isConfigured()
56 function res = isLocked()
59 function configureSimply(logLevel,varargin)
60 import org.apache.log4j.BasicConfigurator;
61 import org.apache.log4j.Level;
62 import org.apache.log4j.Logger;
63 import org.apache.log4j.spi.LoggerRepository;
72 case 'islockafterconfigure' 74 if ~isscalar(isLock) || ~islogical(isLock)
76 'Invalid size or tipe of %s', prop{k});
80 'Property %s is not supported', prop{k});
85 logger=Logger.getLogger(
'mxberry.log.log4j.Log4jConfigurator');
86 logger.warn([
'Attempt to change a locked Log4j configuration', sprintf(
'\n'), ...
88 dbstack(
'-completenames'),...
89 'useHyperlinks',
false,
'prefixStr',
' ')]);
93 %%
set global INFO logging level
97 BasicConfigurator.resetConfiguration();
98 BasicConfigurator.configure();
99 logger=Logger.getRootLogger();
100 repository=logger.getLoggerRepository();
101 repository.setThreshold(Level.(logLevel));
108 function logger=getLogger(loggerName,isSuffix)
109 import org.apache.log4j.Logger;
113 if nargin==0||isSuffix
115 [methodName,className]=...
117 %
delete info on subfunctions
118 curInd=find(methodName==
'/'|methodName==
'\',1,
'first');
120 methodName=methodName(1:curInd-1);
122 if ~isempty(className)
123 className=[className
'.'];
126 loggerName=[className methodName
'.' loggerName];
128 loggerName=[className methodName];
131 logger=Logger.getLogger(loggerName);
133 function lockConfiguration()
134 mxberry.log.log4j.Log4jConfigurator.getSetLockStatus(true);
136 function unlockConfiguration()
137 mxberry.log.log4j.Log4jConfigurator.getSetLockStatus(false);
139 function res = getLastLogPropStr()
140 res =
mxberry.log.log4j.Log4jConfigurator.getSetLogPropStr();
143 methods (Static,Abstract)
144 logFileName=getMainLogFileName()
145 configure(confSource)
147 methods (Access=protected)
148 function logFileName=getMainLogFileNameInternal(self)
149 logFileName=[self.getMainLogFilePathInternal,...
150 self.getShortMainLogFileNameInternal()];
152 function logFileName=getShortMainLogFileNameInternal(self)
153 import
mxberry.log.log4j.Log4jConfigurator;
154 logFileName=[self.MAIN_LOG_FILE_PREFIX,...
155 self.getCurProcessNameInternal(),'.',...
158 function processName=getCurProcessNameInternal(self)
159 import
mxberry.log.log4j.Log4jConfigurator;
162 processName=self.MASTER_LOG_FILE_NAME;
164 curTaskName=['task',num2str(SProp.taskId)];
166 processName=[self.CHILD_LOG_FILE_NAME_PREFIX,'.',...
170 function curPathWithFileSep=getMainLogFilePathInternal(self)
171 metaClass=metaclass(self);
172 curPathWithFileSep=[fileparts(which(metaClass.Name)),...
175 function configureInternal(self,logPropStr,varargin)
176 import org.apache.log4j.Logger;
177 import org.apache.log4j.PropertyConfigurator;
184 isLoggerSuffix = false;
186 switch lower(prop{k})
187 case 'islockafterconfigure' 189 if ~isscalar(isLock) || ~islogical(isLock)
191 'Invalid size or tipe of %s', prop{k});
195 loggerSuffix=prop{k+1};
198 'Property %s is not supported', prop{k});
201 metaClass=metaclass(
self);
202 loggerName=metaClass.Name;
203 if isLoggerSuffix loggerName=[loggerName
'.' loggerSuffix];
207 logger=Logger.getLogger(loggerName);
209 [
'Attempt to change a locked Log4j configuration',...
212 dbstack(
'-completenames'),...
213 'useHyperlinks',
false,
'prefixStr',
' ')]);
216 org.apache.log4j.BasicConfigurator.resetConfiguration();
218 if ~ischar(logPropStr)
220 'configuration source should be a property string');
222 if isempty(logPropStr)
223 throwwarn(
'emptyConfStr',[
'configuration property ',...
224 'string is empty, doing nothing...']);
226 self.getSetLogPropStr(logPropStr);
227 logPropStr=java.lang.String(logPropStr);
229 java.lang.System.setProperty(...
230 self.SP_MAIN_LOG_FILE_NAME,...
231 self.getShortMainLogFileNameInternal);
233 logLocPath=
self.getMainLogFilePathInternal();
239 java.lang.System.setProperty(...
240 self.SP_CUR_PROCESS_NAME,...
241 self.getCurProcessNameInternal);
242 java.lang.System.setProperty(...
243 self.SP_LOG_DIR_WITH_SEP,...
246 java.lang.System.setProperty(...
247 self.SP_LOG_FILE_EXP,...
250 confStream=java.io.ByteArrayInputStream(logPropStr.getBytes());
251 logProp=java.util.Properties();
252 logProp.load(confStream);
253 PropertyConfigurator.configure(logProp);
254 logger=Logger.getLogger(loggerName);
255 logger.info(
'Log4j is successfully configured');
257 self.getSetConfStatus(
true);
259 self.lockConfiguration();
LOG4JCONFIGURATOR simplifies log4j configuration, especially when Parallel Computing Toolbox is used...
function throwerror(in msgTag, in varargin)
THROWERROR works similarly to built-in ERROR function in case when there is no output arguments but s...
static function isLocked()
static function getSetConfStatus(in varargin)
function throwwarn(in msgTag, in varargin)
THROWWARN works similarly to built-in WARNING function in case when there is no output arguments but ...
function gettaskname()
GETTASKNAME returns task name and some additional properties.
function parseparams(in args, in propNameList, in nRegExpected, in nPropExpected)
PARSEPARAMS behaves exactly as a built-in Matlab function apart from the incorrect behavior of Matlab...
static function lockConfiguration()