Git Product home page Git Product logo

logformat's Introduction

logformat

Stringify objects into searchable strings.

Motivation

JSON is a great format for exchanging data, but it isn't so great for logging. Say I want to log the follow user request object:

{
    date: '2015-11-19',
    client: {
        agent: 'firefox',
        ip: '10.1.32.1'
    },
    server: {
        ip: '192.168.2.222'
    }
}

If I use the traditional JSON.stringify(), I get something like this:

{"date":"2015-11-19","client":{"agent":"firefox","ip":"10.1.32.1"},"server":{"ip":"192.168.2.222"}}

That isn't very readable and hard to grep. This library solves those problems by nicely formatting objects as key=value pairs:

date=2015-11-19 client.agent=firefox client.ip=10.1.32.1 server.ip=192.168.2.222

Installation

npm install --save logformat

API

logformat(any [, opts])

Parameters:

  • any anything you wish to stringify... booleans, strings, numbers, objects, arrays, etc.
  • opts options for controlling the behaviour of the function. Object. Optional.
    • maxDepth maximum depth that should be formatted. positive integer. Optional.

Returns:

  • formatted string

NOTE: if any is an object with a circular reference, this function returns '[Circular]';

Example

var logformat = require('logformat');
var fs = require('fs');

console.log(logformat(fs.statSync('/dev/null')));

// -> 'dev=6 mode=8630 nlink=1 uid=0 gid=0 rdev=259 blksize=4096 ino=1029 size=0 blocks=0 atime=2017-07-12T00:21:34-04:00 mtime=2017-07-12T00:21:34-04:00 ctime=2017-07-12T00:21:34-04:00 birthtime=2017-07-12T00:21:34-04:00'

Testing

There is an automated test suite:

npm test

License

See LICENSE.md

logformat's People

Contributors

tcort avatar

Stargazers

Andreas Kohn avatar Jimmy Huang avatar Vlad Trukhin avatar Brandon Pierce avatar Ian Storm Taylor avatar  avatar

Watchers

 avatar James Cloos avatar

Forkers

tcort

logformat's Issues

logformat assumes innerVal is not an Object.

logformat assumes innerVal is not an Object. Does not process innerVal if innerVal is an Object. This doesn't provide a deep reformat of JSON to key-value pairs
Take for example: {"date":"2015-11-19","client":{"agent":"firefox","ip":"10.1.32.1"},"server":{"windows":{"ip":"192.168.2.222"}}}
Parse JSON to the last primitive/simple value

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.