Last active
July 27, 2022 15:02
-
-
Save timdown/6572000 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Rough FileAppender implementation for log4javascript 1.4. | |
* | |
* Copyright 2013 Tim Down. | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
(function() { | |
function BrowserFileAppender(fileName) { | |
fileName = fileName || "log4javascript.log"; | |
var appender = this; | |
// Use HTML5 file API if available | |
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; | |
if (window.requestFileSystem && window.Blob) { | |
appender.writeMessageToFile = (function() { | |
var queuedMessages = []; | |
var errorHandler = function(err) { | |
var msg = ''; | |
switch (err.code) { | |
case FileError.QUOTA_EXCEEDED_ERR: | |
msg = "QUOTA_EXCEEDED_ERR"; | |
break; | |
case FileError.NOT_FOUND_ERR: | |
msg = "NOT_FOUND_ERR"; | |
break; | |
case FileError.SECURITY_ERR: | |
msg = "SECURITY_ERR"; | |
break; | |
case FileError.INVALID_MODIFICATION_ERR: | |
msg = "INVALID_MODIFICATION_ERR"; | |
break; | |
case FileError.INVALID_STATE_ERR: | |
msg = "INVALID_STATE_ERR"; | |
break; | |
default: | |
msg = "Unknown Error"; | |
break; | |
} | |
if (window.console && window.console.error) { | |
console.error("An error occurred: " + msg, arguments); | |
} | |
}; | |
var appendQueuedMessages; | |
var writing = false; | |
window.requestFileSystem( | |
window.TEMPORARY, | |
5 * 1024 * 1024, | |
function(fs) { | |
if (window.console && window.console.log) { | |
console.log("File system object initialized!"); | |
} | |
fs.root.getFile(fileName, {create: true}, function(fileEntry) { | |
appendQueuedMessages = function() { | |
writing = true; | |
fileEntry.createWriter(function(fileWriter) { | |
fileWriter.seek(fileWriter.length); | |
var blob = new Blob(queuedMessages, {type: 'text/plain'}); | |
fileWriter.write(blob); | |
queuedMessages.length = 0; | |
writing = false; | |
}, errorHandler); | |
}; | |
}, errorHandler); | |
}, | |
errorHandler | |
); | |
return function(msg) { | |
queuedMessages.push(msg); | |
if (appendQueuedMessages && !writing) { | |
appendQueuedMessages(); | |
} | |
}; | |
})(); | |
} | |
// Next strategy: ActiveX, as used in IE. | |
else if (typeof window.ActiveXObject != "undefined") { | |
appender.writeMessageToFile = (function() { | |
var fso; | |
var queuedMessages = []; | |
var timer; | |
var writeMessageToFile = null; | |
try { | |
fso = new ActiveXObject("Scripting.FileSystemObject"); | |
var appendQueuedMessages = function() { | |
timer = null; | |
// Try opening existing file and create it first if it does not exist | |
try { | |
var file = fso.OpenTextFile(fileName, 8, true); | |
file.Write( queuedMessages.join("") ); | |
file.close(); | |
queuedMessages.length = 0; | |
} catch (e) { | |
// Prevent further attempts to write to this file | |
appender.writeMessageToFile = null; | |
} | |
}; | |
writeMessageToFile = function(msg) { | |
queuedMessages.push(msg); | |
if (timer) { | |
window.clearTimeout(timer); | |
} | |
timer = window.setTimeout(appendQueuedMessages, 200); | |
}; | |
} catch(e) { | |
} | |
return writeMessageToFile; | |
})(); | |
} | |
} | |
BrowserFileAppender.prototype = new log4javascript.Appender(); | |
BrowserFileAppender.prototype.layout = new log4javascript.PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p - %m{1}%n"); | |
BrowserFileAppender.prototype.append = function(loggingEvent) { | |
if (this.writeMessageToFile) { | |
this.writeMessageToFile( this.getLayout().formatWithException(loggingEvent) ); | |
} | |
}; | |
BrowserFileAppender.prototype.toString = function() { | |
return "BrowserFileAppender"; | |
}; | |
log4javascript.BrowserFileAppender = BrowserFileAppender; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment