1 var define = require("../../define.js").define,
  2 		base = require("../../base"),
  3 		string = base.string,
  4 		escape = base.regexp.escapeString,
  5 		FileAppender = require("./fileAppender"),
  6 		format = string.format,
  7 		Level = require("../level"),
  8 		fs = require("fs");
  9 
 10 
 11 /**
 12  * @class Appends messages to a file in JSON format. The messages are logged to an array in a JSON file
 13  * <b>The file is always overwritten</b>
 14  *
 15  * <pre class="code">
 16  * //example log.json
 17  * [
 18  *    {
 19  *      "timestamp" : "Wed Jun 08 2011 11:16:20 GMT-0500 (CDT)",
 20  *      "level" : "INFO",
 21  *      "name" : "comb",
 22  *      "message" :  "INFO MESSAGE!!!!"
 23  *    }
 24  *  ]
 25  *
 26  *</pre>
 27  *
 28  * @name JSONAppender
 29  * @augments comb.logging.appenders.FileAppender
 30  * @memberOf comb.logging.appenders
 31  *
 32  * @param {Object} [options] options to assign to this Appender
 33  * @param {String} [options.name="appender"] the name of this Appender. If you want two of the same type of appender
 34  *                                           on a logger it must have a different name.
 35  * @param {String} [options.pattern="{"timestamp" : "{timeStamp}", "level" : "{levelName}", "name" : "{name}", "message" :  "{message}"}"]
 36  *  <p>Available Options for formatting see {@link comb.string.format} for formatting options</p>
 37  *  <ul>
 38  *      <li>timeStamp - the timestamp of the event being logged</li>
 39  *      <li>level - the {@link comb.logging.Level} of the event</li>
 40  *      <li>levelName - the name of the level being logged</li>
 41  *      <li>name - the name of the logger logging the event</li>
 42  *      <li>message - the message being logged</li>
 43  * </ul>
 44  * @param {comb.logging.Level|String} [options.level=comb.logging.Level.INFO] the logging level of this appender
 45  *      <p><b>Note:</b> the level can be different from the logger in the case that you want a particular logger
 46  *      to only log particular event of a level. For example an appender that only logs errors. BEWARE that if the
 47  *      appenders level is lower than the logger is will not recieve any messages.</p>
 48  *
 49  * @param {String} [options.file="./log.json"] the file to log events to.
 50  * @param {String} [options.encoding="utf8"] the encoding of the file.
 51  */
 52 exports = module.exports = define(FileAppender, {
 53 			instance : {
 54 
 55 				constructor : function(options) {
 56 					options = options || {};
 57 					this.name = options.name || "JSONAppender";
 58 					this.__count = 0;
 59 					this.__file = options.file || "./log.json";
 60 					this.__encoding = options.encoding || "utf8";
 61 					this.__writeStream = options.writeStream || fs.createWriteStream(this.__file, { flags: "w", encoding: this.__encoding});
 62 					this.__writeStream.write("[\n");
 63 					this.__level = options.level;
 64 					//explicit overwrite of patter
 65 					this.__pattern = '{"timestamp" : "{timeStamp}", "level" : "{levelName}", "name" : "{name}", "message" :  "{message}"}';
 66 					process.addListener("exit", base.hitch(this, "__onExit"));
 67 				},
 68 
 69 				append : function(event) {
 70 					if (this._canAppend(event)) {
 71 						event.message = event.message.replace(/\n+/g, "\\n");
 72 						var message = (this.__count ? ",\n" : "\n") + format(this.__pattern, event);
 73                         this.__writeStream.write(message);
 74 						this.__count++;
 75 					}
 76 				},
 77 
 78 
 79 				__onExit : function() {
 80 					this.__writeStream.write("]");
 81 					this.super(arguments);
 82 				}
 83 			}
 84 		});