super.init(argumentCollection=arguments);
// Setup Properties
if( NOT propertyExists("filepath") ){
throw(message="Filepath property not defined",type="FileAppender.PropertyNotFound");
}
if( NOT propertyExists("autoExpand") ){
setProperty("autoExpand",true);
}
if( NOT propertyExists("filename") ){
setProperty("filename",getName());
}
if( NOT propertyExists("fileEncoding") ){
setProperty("fileEncoding","ISO-8859-1");
}
// Cleanup File Names
setProperty("filename", REreplacenocase(getProperty("filename"), "[^0-9a-z]","","ALL") );
// Setup the log file full path
instance.logFullpath = getProperty("filePath");
// Clean ending slash
instance.logFullPath = reReplacenocase(instance.logFullPath,"[/\\]$","");
// Concatenate Full Log path
instance.logFullPath = instance.logFullpath & "/" & getProperty("filename") & ".log";
// Do we expand the path?
if( getProperty("autoExpand") ){
instance.logFullPath = expandPath(instance.logFullpath);
}
//lock information
instance.lockName = instance._hash & getname() & "logOperation";
instance.lockTimeout = 25;
return this;
// Default Log Directory
ensureDefaultLogDirectory();
// Init the log location
initLogLocation();
var loge = arguments.logEvent;
var timestamp = loge.getTimestamp();
var message = loge.getMessage();
var entry = "";
// Does file still exist?
if( NOT fileExists( instance.logFullpath ) ){
ensureDefaultLogDirectory();
initLogLocation();
}
if( hasCustomLayout() ){
entry = getCustomLayout().format(loge);
}
else{
// Cleanup main message
message = replace(message,'"','""',"all");
message = replace(message,"#chr(13)##chr(10)#",' ',"all");
message = replace(message,chr(13),' ',"all");
if( len(loge.getExtraInfoAsString()) ){
message = message & " " & loge.getExtraInfoAsString();
}
// Entry string
entry = '"#severityToString(logEvent.getSeverity())#","#getname()#","#dateformat(timestamp,"MM/DD/YYYY")#","#timeformat(timestamp,"HH:MM:SS")#","#loge.getCategory()#","#message#"';
}
// Setup the real entry
append(entry);
// Double Lock
if( not fileExists( instance.logFullpath ) ){
// Create empty log file
try{
fileObj = createObject("java","java.io.File").init(JavaCast("string", instance.logFullpath )).createNewFile();
}
catch(Any e){
$log("ERROR","Cannot create appender's: #getName()# log file. File #instance.logFullpath#. #e.message# #e.detail#");
}
}
//Check if we can write
fileObj = createObject("java","java.io.File").init(JavaCast("string",instance.logFullpath));
if( NOT fileObj.canWrite() ){
$log("ERROR","Cannot write to file: #instance.logFullpath# by appender #getName()#");
}
// if necessary we create a thread that takes care for messages --->
if(!isDefined("application.logListener['#instance._hash#'].active") || !application.logListener['#instance._hash#'].active) // TODO: choose a variable name that matching the coldbox pattern
startLogListener();
else
systemOutput("use existing listener",true,true);
// send message
arrayAppend(application.logListener['#instance._hash#'].queue,arguments.message);
systemOutput("start new listener",true,true);
if(!isDefined("application.logListener['#instance._hash#']"))// TODO: choose a variable name that matching the coldbox pattern
application.logListener['#instance._hash#']={active:true,queue:[]}; // TODO: for ACF we need to make sure the array is synchronized
else
application.logListener['#instance._hash#'].active=true;
// create the base structure if necessary
// looking for messages in the queue
last=getTickCount();
start=last;
maxIdle=15000;
flushInterval=1000;
interval=50;
count=0;
local.fo=fileOpen(attributes.file,"append");
hasMessages=false;
try{
// do we have messages or still idle timeout is not reached
while((hasMessages=arrayLen(application.logListener['#attributes.id#'].queue)) || last+maxIdle>getTickCount()) {
if(hasMessages){
msg=application.logListener['#attributes.id#'].queue[1];
arrayDeleteAt(application.logListener['#attributes.id#'].queue,1);
if(isSimpleValue(local.fo))
local.fo=fileOpen(attributes.file,"append"); // was closed?
FileWrite( fo, msg , attributes.charset);
//systemOutput(msg&"count:"&(count),true,true);
last=getTickCount();
}
// flush?
if(start+flushInterval