Code Monkey home page Code Monkey logo

gelf's Introduction

log4js-node CodeQL Node.js CI

NPM

This is a conversion of the log4js framework to work with node. I started out just stripping out the browser-specific code and tidying up some of the javascript to work better in node. It grew from there. Although it's got a similar name to the Java library log4j, thinking that it will behave the same way will only bring you sorrow and confusion.

The full documentation is available here.

Changes in version 3.x

There have been a few changes between log4js 1.x and 2.x (and 0.x too). You should probably read this migration guide if things aren't working.

Out of the box it supports the following features:

  • coloured console logging to stdout or stderr
  • file appender, with configurable log rolling based on file size or date
  • a logger for connect/express servers
  • configurable log message layout/patterns
  • different log levels for different log categories (make some parts of your app log as DEBUG, others only ERRORS, etc.)

Optional appenders are available:

Getting help

Having problems? Jump on the slack channel, or create an issue. If you want to help out with the development, the slack channel is a good place to go as well.

installation

npm install log4js

usage

Minimalist version:

var log4js = require("log4js");
var logger = log4js.getLogger();
logger.level = "debug";
logger.debug("Some debug messages");

By default, log4js will not output any logs (so that it can safely be used in libraries). The level for the default category is set to OFF. To enable logs, set the level (as in the example). This will then output to stdout with the coloured layout (thanks to masylum), so for the above you would see:

[2010-01-17 11:43:37.987] [DEBUG] [default] - Some debug messages

See example.js for a full example, but here's a snippet (also in examples/fromreadme.js):

const log4js = require("log4js");
log4js.configure({
  appenders: { cheese: { type: "file", filename: "cheese.log" } },
  categories: { default: { appenders: ["cheese"], level: "error" } },
});

const logger = log4js.getLogger("cheese");
logger.trace("Entering cheese testing");
logger.debug("Got cheese.");
logger.info("Cheese is Comté.");
logger.warn("Cheese is quite smelly.");
logger.error("Cheese is too ripe!");
logger.fatal("Cheese was breeding ground for listeria.");

Output (in cheese.log):

[2010-01-17 11:43:37.987] [ERROR] cheese - Cheese is too ripe!
[2010-01-17 11:43:37.990] [FATAL] cheese - Cheese was breeding ground for listeria.

Note for library makers

If you're writing a library and would like to include support for log4js, without introducing a dependency headache for your users, take a look at log4js-api.

Documentation

Available here.

There's also an example application.

TypeScript

import * as log4js from "log4js";
log4js.configure({
  appenders: { cheese: { type: "file", filename: "cheese.log" } },
  categories: { default: { appenders: ["cheese"], level: "error" } },
});

const logger = log4js.getLogger();
logger.level = "debug";
logger.debug("Some debug messages");

Contributing

We're always looking for people to help out. Jump on slack and discuss what you want to do. Also, take a look at the rules before submitting a pull request.

License

The original log4js was distributed under the Apache 2.0 License, and so is this. I've tried to keep the original copyright and author credits in place, except in sections that I have rewritten extensively.

gelf's People

Contributors

andyschwarzl avatar dependabot[bot] avatar lamweili avatar nomiddlename avatar sockethe2nd avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

gelf's Issues

short_message not appearing

I'm in the process of testing out log4js-node as a replacement for bunyan, and checking that the gelf transport works as expected.

I'm using gelf to send data to logstash. It's working for the most part, except that short_message doesn't appear in kibana. If I change the code to use _short_message instead, it works as expected. I've tried several different layouts - currently testing with dummy, but I've tested basic and the default.

My apender looks like this:

gelf: {type: '@log4js-node/gelf', host: 'xxx', port: 12201, facility: 'log-test', layout: {type: 'dummy'}, customFields: {_foo: 'bar'}}

This is really weird. The facility property gets sent correctly, timestamp, host, level etc all work.

According to the logstash docs short_message is expected, and it works with the existing bunyan setup.

I can't see any important difference between the code in this repo vs the gelf-stream package used for bunyan.

I also checked gelf-stream's dep gelfling and it's not adding the _.

I am still looking for configuration issues that could be causing this, but I'm pretty confused because the messages are being sent to a different index but the same server, with all the same config as the working bunyan version.

Any ideas?

Not Able to specify timestamp with the message

Hi,
I am using @log4js-node/[email protected]. I am sending a message with timestamp info, which is not being picked up by the module.

const log4js = require('log4js');

log4js.configure({
  appenders: {
    gelf: { type: '@log4js-node/gelf', host: 'localhost', port: '12201', }
  },
  categories: {
    default: { appenders: ['gelf'], level: 'info'  }
  }
});

module.exports = log4js;

const logger = log4js.getLogger();

logger.info({ GELF: true, _tool: 'proxy-parser', _uniqueId: 'test', _timestamp: 1611283489  }, 'very very old message');

But the message is getting logged with the current timestamp on graylog.

Looks like the issue with this line

msg.timestamp = msg.timestamp || new Date().getTime() / 1000; // log should use millisecond

It should have been

msg.timestamp = msg._timestamp || new Date().getTime() / 1000; // log should use millisecond

Can this be fixed?

Thank You,
Phani

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.