Code Monkey home page Code Monkey logo

ezlogger's Introduction

ezLogger

Installation

npm install ezLogger

Initialize

After initialize all project's console.log will change.

var ezLogger = require("ezlogger");
ezLogger(__dirname + "/log.txt");
console.log("That's it!");
// ezLogger(FileName,Pattern);
// default pattern is
// "[date] [file]:[line] [message]"

Note: Pattern is updated in 1.3.0 to function

ezLogger("./log.txt", function(file, line, message) {
   return "{3} {0}:{1} {2}".format(file, line, message, Date.simple());
});

log.txt file appeared.

15/06/2015 15:39:31 app.js:3 That's it!

ezFormat

You can use console.log with ezFormat.

// initialize
var ezLogger = require("ezlogger");
ezLogger(__dirname + "/log.txt");
var isConnected = true;
var myObject = {
    Life: 1,
    Tools: [
        {
            Name: "Jacket",
            Weight: 3,
        }
    ]
};
console.log("Server connection: {0:bool}", isConnected);
console.log("Returned object {0:json:4}", myObject);

Dynamic File

Initialize ezlogger with function

var ezLogger = require("ezlogger");
ezLogger(function() {
    // you can use Date.simple.more too. It will come with ezlogger
    return __dirname + "/log." + Date.simple.more() + ".txt";
});
console.log("That's it!");

log.15.06.2015.txt file appeared.

15/06/2015 15:39:31 app.js:5 That's it!

Trace Level

If you want to create own logger function, you probably get wrong location because of trace level is 1 default. So use logTrace

function myLoggerError(message) {
    console.log("[ERROR] {0}", message);
}

myLoggerError("test"); // prints 15/06/2015 15:39:31 test.js:2 [ERROR] test
// but it shouldn't be test.js:2 it should be test.js:5

with logTrace

function myLoggerError(message) {
    console.logTrace(2, "[ERROR] {0}", message);
}

myLoggerError("test"); // prints 15/06/2015 15:39:31 test.js:5 [ERROR] test

add ezFormat to own function

function myLoggerError(message) {
    if (arguments.length > 1) {
          message = String.prototype.format.apply(message, Array.prototype.slice.call(arguments, 1));
    }
    
    console.logTrace(2, "[ERROR] {0}", message);
}

myLoggerError("test! {0}", 1); // prints  15/06/2015 15:39:31 test.js:9 [ERROR] test! 1

1.6.0 Features

  • console.error added
  • console.errorTrace added
  • handleUncaughtExceptions added.
var ezLogger = require("ezlogger")();
ezLogger.handleUncaughtExceptions();

1.6.1 Features

  • ezlogger.express
var express = require("express");
var ezlogger = require("ezlogger");
var app = express();

app.use(ezlogger.express);
app.get("/", function(req, res) {
    res.send("ok");
});

app.listen(80);

ezlogger's People

Contributors

co3moz avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

gitter-badger

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.