Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including node-logs with all npm packages installed. Try it out:

var nodeLogs = require("node-logs")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

node-logs v1.0.4

A class to manage logs

node-logs

A class to manage logs

Build status Coverage status Dependency status Dev dependency status Issues Pull requests

Be careful !!! With this version, the plugin became absolutly agnostic and does not support sqlite anymore. Please refer to node-logs-sqlite for that.

Installation

$ npm install node-logs

Features

  • Easily manage your logs in the way you want
  • Show logs in the command prompt
  • Add interfaces to use new specific way to log data (api, json, etc...)

Doc

-- Interfaces --

type iOption = "background" | "bold" | "italic" | "strikethrough" | "underline";

interface iInterface {
  log: Function;
  success: Function;
  information: Function;
  warning: Function;
  error: Function;
}

-- Constructor --

  • constructor ()

-- Methods --

  • -- Accessors --

  • showInConsole (showInConsole: boolean): this default false

  • -- Init / Release --

  • init(): Promise< resolve<void> | reject<Error> >

  • release(): Promise< resolve<void> | reject<Error> >

  • -- Interfaces --

  • addInterface(interface: iInterface): Promise< resolve<void> | reject< ReferenceError|TypeError|Error > > add your own way to log data

  • -- Write logs --

  • log(text: any, option?: Array<iOption>) : Promise< resolve<void> | reject<Error> >

  • success(text: any, option?: Array<iOption>): Promise< resolve<void> | reject<Error> > alias : "ok"

  • warning(text: any, option?: Array<iOption>): Promise< resolve<void> | reject<Error> > alias : "warn"

  • error(text: any, option?: Array<iOption>): Promise< resolve<void> | reject<Error> > alias : "err"

  • information(text: any, option?: Array<iOption>): Promise< resolve<void> | reject<Error> > alias : "info"

Examples

Native

const Logs = require("node-logs");
const logs = new Logs();
logs.showInConsole(true);
// example: link the logger to a Web API
const { request } = require("http");

function _myOwnLogger(type, msg) {

  const req = request({
    "host": "www.myownloger.com",
    "method": "PUT",
    "path": "/api/" + type + "/"
  }, (res) => {

    res.setEncoding("utf8");

    let data = "";
    res.on("data", (chunk) => {
      data += chunk;
    }).on("end", () => {
      console.log(data);
    });

  });

  req.write(String(msg));
  req.end();

}

logs.addInterface({

  "log" : (msg, options) => { _myOwnLogger("log", msg, options); },
  "success" : (msg, options) => { _myOwnLogger("success", msg, options); },
  "information" : (msg, options) => { _myOwnLogger("information", msg, options); },
  "warning" : (msg, options) => { _myOwnLogger("warning", msg, options); },
  "error" : (msg, options) => { _myOwnLogger("error", msg, options); }

}).then(() => {
  console.log("MyOwnLoger added !");
}).catch((err) => {
  console.error(err);
});
return logs.init().then(() => {

  // you can use the logger in a classical way

  logs.log("log");
  logs.success("success"); logs.ok("ok");
  logs.information("information");
  logs.warning("warning"); logs.warn("warn");
  logs.error("error"); logs.err("err");

  // or with promises if you added an asynchronous interface

  logs.log("log").then(() => {

     return logs.ok("ok").then(() => {
        return logs.success("success");
     });

  }).then(() => {

     return logs.warn("warn").then(() => {
        return logs.warning("warning");
     });

  }).then(() => {

     return logs.err("err").then(() => {
        return logs.error("error");
     });

  }).then(() => {
     return logs.information("information");
  }).catch((err) => {
     console.log(err);
  });

  // you can also add "iOption" style features

  logs.log("log", [ "bold", "underline" ]);

  // release

  logs.release().catch((err) => {
     console.log(err);
  });

});

Typescript

import Logs = require("node-logs");
const logs = new Logs();

logs.init().then(() => {
   return logs.log("log");
});

Tests

$ npm run-script tests

License

ISC

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free