1 | # log4js-node [![Build Status](https://secure.travis-ci.org/log4js-node/log4js-node.png?branch=master)](http://travis-ci.org/log4js-node/log4js-node) [![codecov](https://codecov.io/gh/log4js-node/log4js-node/branch/master/graph/badge.svg)](https://codecov.io/gh/log4js-node/log4js-node)
|
---|
2 |
|
---|
3 | [![NPM](https://nodei.co/npm/log4js.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/log4js/)
|
---|
4 |
|
---|
5 | This is a conversion of the [log4js](https://github.com/stritti/log4js)
|
---|
6 | framework to work with [node](http://nodejs.org). 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](https://logging.apache.org/log4j/2.x/), thinking that it will behave the same way will only bring you sorrow and confusion.
|
---|
7 |
|
---|
8 | The full documentation is available [here](https://log4js-node.github.io/log4js-node/).
|
---|
9 |
|
---|
10 | [Changes in version 3.x](https://log4js-node.github.io/log4js-node/v3-changes.md)
|
---|
11 |
|
---|
12 | There have been a few changes between log4js 1.x and 2.x (and 0.x too). You should probably read this [migration guide](https://log4js-node.github.io/log4js-node/migration-guide.html) if things aren't working.
|
---|
13 |
|
---|
14 | Out of the box it supports the following features:
|
---|
15 |
|
---|
16 | - coloured console logging to stdout or stderr
|
---|
17 | - file appender, with configurable log rolling based on file size or date
|
---|
18 | - a logger for connect/express servers
|
---|
19 | - configurable log message layout/patterns
|
---|
20 | - different log levels for different log categories (make some parts of your app log as DEBUG, others only ERRORS, etc.)
|
---|
21 |
|
---|
22 | Optional appenders are available:
|
---|
23 |
|
---|
24 | - [SMTP](https://github.com/log4js-node/smtp)
|
---|
25 | - [GELF](https://github.com/log4js-node/gelf)
|
---|
26 | - [Loggly](https://github.com/log4js-node/loggly)
|
---|
27 | - Logstash ([UDP](https://github.com/log4js-node/logstashUDP) and [HTTP](https://github.com/log4js-node/logstashHTTP))
|
---|
28 | - logFaces ([UDP](https://github.com/log4js-node/logFaces-UDP) and [HTTP](https://github.com/log4js-node/logFaces-HTTP))
|
---|
29 | - [RabbitMQ](https://github.com/log4js-node/rabbitmq)
|
---|
30 | - [Redis](https://github.com/log4js-node/redis)
|
---|
31 | - [Hipchat](https://github.com/log4js-node/hipchat)
|
---|
32 | - [Slack](https://github.com/log4js-node/slack)
|
---|
33 | - [mailgun](https://github.com/log4js-node/mailgun)
|
---|
34 | - [InfluxDB](https://github.com/rnd-debug/log4js-influxdb-appender)
|
---|
35 |
|
---|
36 | ## Getting help
|
---|
37 |
|
---|
38 | Having problems? Jump on the [slack](https://join.slack.com/t/log4js-node/shared_invite/enQtODkzMDQ3MzExMDczLWUzZmY0MmI0YWI1ZjFhODY0YjI0YmU1N2U5ZTRkOTYyYzg3MjY5NWI4M2FjZThjYjdiOGM0NjU2NzBmYTJjOGI) 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.
|
---|
39 |
|
---|
40 | ## installation
|
---|
41 |
|
---|
42 | ```bash
|
---|
43 | npm install log4js
|
---|
44 | ```
|
---|
45 |
|
---|
46 | ## usage
|
---|
47 |
|
---|
48 | Minimalist version:
|
---|
49 |
|
---|
50 | ```javascript
|
---|
51 | var log4js = require("log4js");
|
---|
52 | var logger = log4js.getLogger();
|
---|
53 | logger.level = "debug";
|
---|
54 | logger.debug("Some debug messages");
|
---|
55 | ```
|
---|
56 |
|
---|
57 | 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](http://github.com/masylum)), so for the above you would see:
|
---|
58 |
|
---|
59 | ```bash
|
---|
60 | [2010-01-17 11:43:37.987] [DEBUG] [default] - Some debug messages
|
---|
61 | ```
|
---|
62 |
|
---|
63 | See example.js for a full example, but here's a snippet (also in `examples/fromreadme.js`):
|
---|
64 |
|
---|
65 | ```javascript
|
---|
66 | const log4js = require("log4js");
|
---|
67 | log4js.configure({
|
---|
68 | appenders: { cheese: { type: "file", filename: "cheese.log" } },
|
---|
69 | categories: { default: { appenders: ["cheese"], level: "error" } }
|
---|
70 | });
|
---|
71 |
|
---|
72 | const logger = log4js.getLogger("cheese");
|
---|
73 | logger.trace("Entering cheese testing");
|
---|
74 | logger.debug("Got cheese.");
|
---|
75 | logger.info("Cheese is Comté.");
|
---|
76 | logger.warn("Cheese is quite smelly.");
|
---|
77 | logger.error("Cheese is too ripe!");
|
---|
78 | logger.fatal("Cheese was breeding ground for listeria.");
|
---|
79 | ```
|
---|
80 |
|
---|
81 | Output (in `cheese.log`):
|
---|
82 |
|
---|
83 | ```bash
|
---|
84 | [2010-01-17 11:43:37.987] [ERROR] cheese - Cheese is too ripe!
|
---|
85 | [2010-01-17 11:43:37.990] [FATAL] cheese - Cheese was breeding ground for listeria.
|
---|
86 | ```
|
---|
87 |
|
---|
88 | ## Note for library makers
|
---|
89 |
|
---|
90 | 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](https://github.com/log4js-node/log4js-api).
|
---|
91 |
|
---|
92 | ## Documentation
|
---|
93 |
|
---|
94 | Available [here](https://log4js-node.github.io/log4js-node/).
|
---|
95 |
|
---|
96 | There's also [an example application](https://github.com/log4js-node/log4js-example).
|
---|
97 |
|
---|
98 | ## TypeScript
|
---|
99 |
|
---|
100 | ```ts
|
---|
101 | import { configure, getLogger } from "log4js";
|
---|
102 | configure("./filename");
|
---|
103 | const logger = getLogger();
|
---|
104 | logger.level = "debug";
|
---|
105 | logger.debug("Some debug messages");
|
---|
106 |
|
---|
107 | configure({
|
---|
108 | appenders: { cheese: { type: "file", filename: "cheese.log" } },
|
---|
109 | categories: { default: { appenders: ["cheese"], level: "error" } }
|
---|
110 | });
|
---|
111 | ```
|
---|
112 |
|
---|
113 | ## Contributing
|
---|
114 |
|
---|
115 | We're always looking for people to help out. Jump on [slack](https://join.slack.com/t/log4js-node/shared_invite/enQtODkzMDQ3MzExMDczLWUzZmY0MmI0YWI1ZjFhODY0YjI0YmU1N2U5ZTRkOTYyYzg3MjY5NWI4M2FjZThjYjdiOGM0NjU2NzBmYTJjOGI) and discuss what you want to do. Also, take a look at the [rules](https://log4js-node.github.io/log4js-node/contrib-guidelines.html) before submitting a pull request.
|
---|
116 |
|
---|
117 | ## License
|
---|
118 |
|
---|
119 | The original log4js was distributed under the Apache 2.0 License, and so is this. I've tried to
|
---|
120 | keep the original copyright and author credits in place, except in sections that I have rewritten
|
---|
121 | extensively.
|
---|