views:

38

answers:

2

Hi,

i've a JavaScript application.

I'd like to use a logger but instead of logging to a console (like Firebug or Yahoo UI), I'd like to log to a file or several files.

Ideally, It should be possible to back up the files when they reach a certain limit.

thanks.

+1  A: 

You may want to check out log4js. It is a logging API based on the popular log4j framework. It is an open source project, using the Apache License 2.0.

You may also want to check out the log4javascript project, written by the Stack Overflow regular @Tim Down.

Daniel Vassallo
there's no rollingfile appender right ?
LB
@LB: No there doesn't seem to be one.
Daniel Vassallo
+2  A: 

Writing to a file on the client machine is not generally possible via JavaScript running in a web page. It can be done in IE using ActiveX (when the user has allowed it) and I think also in Firefox when the user has enabled an obscure setting, but is generally not possible. There are various questions about this in Stack Overflow. For example:

Bearing that in mind, it has never seemed worthwhile implementing a local file appender in log4javascript. If you do want to log to a file, I would suggest using the AjaxAppender to send log messages to the server and log those messages to log4j / log4net / log4php / whatever set up with a rolling file appender.

Tim Down
+1... Good idea about the AjaxAppender and log4x mix... By the way, kudos for log4javascript :)
Daniel Vassallo
I've read an article about the AjaxAppender to send messages, it seems to be a good idea. However, I don't want to send messages over the network again and again. There's a FileAppender in Log4Js (i haven't tested it though), but no rollingFile. Maybe I should try using the ActiveX ..
LB
FileAppender in log4js uses ActiveX in IE and built-in code in Firefox that requires the user to have enabled a setting. If this is a reasonable solution for you then I'd suggest using it. It shouldn't be too hard to adapt it to do the RollingFileAppender stuff. Otherwise, AjaxAppender in log4javascript does allow you to send log messages in batches of a particular size or at particular timed intervals.
Tim Down