WWW-Mechanize-PhantomJS

 view release on metacpan or  search on metacpan

lib/WWW/Mechanize/PhantomJS/ghostdriver/logger.js  view on Meta::CPAN

separator = " - ";

/**
 * (Super-simple) Logger
 *
 * @param context {String} Logger context
 */
function Logger (context) {
    var loggerObj, i;

    if (!context || context.length === 0) {
        throw new Error("Invalid 'context' for Logger: " + context);
    }

    loggerObj = {
        debug   : function(scope, message) {
            console.debug(context + separator +
                scope +
                (message && message.length > 0 ? separator + message : "")
            );
        },
        info    : function(scope, message) {
            console.info(context + separator +
                scope +
                (message && message.length > 0 ? separator + message : "")
            );
        },
        warn    : function(scope, message) {
            console.warn(context + separator +
                scope +
                (message && message.length > 0 ? separator + message : "")
            );
        },
        error   : function(scope, message) {
            console.error(context + separator +
                scope +
                (message && message.length > 0 ? separator + message : "")
            );
        }
    };


    return loggerObj;
}

/**
 * Export: Create Logger with Context
 *
 * @param context {String} Context of the new Logger
 */
exports.create = function (context) {
    return new Logger(context);
};

/**
 * Export: Add Log File.
 *
 * @param logFileName {String Name of the file were to output (append) the Logs.
 */
exports.addLogFile = function(logFileName) {
    var fs = require("fs"),
        f = fs.open(fs.absolute(logFileName), 'a');

    // Append line to Log File
    console.onOutput(function(msg, levelName) {
        f.writeLine(msg);
        f.flush();
    });

    // Flush the Log File when process exits
    phantom.aboutToExit.connect(f.flush);
};

/**
 * Export: Console object
 */
exports.console = console;



( run in 1.539 second using v1.01-cache-2.11-cpan-39bf76dae61 )