Skip to content
This repository was archived by the owner on Aug 6, 2025. It is now read-only.

Commit 2ed380c

Browse files
committed
Move to 0.1.1
Updated documentation and progressed version number
1 parent 6cf7f45 commit 2ed380c

File tree

9 files changed

+127
-75
lines changed

9 files changed

+127
-75
lines changed

README.md

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,31 @@ A fast, lightweight (~12KB compressed) JavaScript logger with no runtime depende
33

44
## Setup
55

6-
Simply require the log4js
6+
Simply require the log4js2 module.
77

88
```javascript
99
var log4js = require('log4js2');
1010
```
1111

12-
Or, for HTML implementations, place the log4js distirbution in your HTML ```<head>``` tag.
12+
Or, for HTML implementations, place the log4js distribution in your HTML ```<head>``` tag.
1313

1414
```html
1515
<script type="text/javascript" src="log4js2.min.js"></script>
1616
```
1717

18+
Then, log some stuff!!
19+
20+
```javascript
21+
22+
// create the logger
23+
var log = log4js.getLogger('myLogger');
24+
25+
// log an event
26+
log.info('This is a log');
27+
28+
// output: "03-24-2016 12:00:18,670|myLogger:anonymous:3|This is a log"
29+
```
30+
1831
## Configuration
1932

2033
Configure log4js using the configure() method. This must be the first thing you do. Otherwise, the first log you commit will not allow updates from this function
@@ -30,23 +43,49 @@ log4js.configure({
3043
});
3144
```
3245

33-
Log some stuff
46+
### Configuration Options
47+
48+
#### allowAppenderInjection
49+
Type: `Boolean`
50+
Default: `false`
51+
52+
Turn on or off the ability to inject appenders. If set to false, no appenders can be added after the first log. This may be useful if you need to add an appender during runtime.
53+
54+
#### appenders
55+
Type: `Array.<String>`
56+
Default: `[ 'consoleAppender' ]`
57+
58+
Sets the appenders for the given log configuration. Packaged with log4js2 is the console appender. You can develop your own appenders to add more functionality.
59+
60+
#### loggers
61+
Type: `Array.<Object>`
62+
Default: `[{ tag : 'main', logLevel : log4js.LogLevel.INFO }]`
63+
64+
Sets the loggers for log4js2. The `tag` property specifies what logger the appender pertains to (see below), and the `logLevel` specifies the logging level (use `log4js.LogLevel`).
3465

3566
```javascript
67+
log4js.configure({
68+
// ...
69+
loggers : [ {
70+
logLevel : log4js.LogLevel.INFO
71+
}, {
72+
tag : 'debugLogger',
73+
logLevel : log4js.LogLevel.DEBUG
74+
} ]
75+
});
3676

37-
// create the logger
3877
var log = log4js.getLogger('myLogger');
78+
var debugLog = log4js.getLogger('debugLogger');
3979

40-
// log an event
41-
log.info('This is a log');
42-
43-
// output: "03-24-2016 12:00:18,670|myLogger:anonymous:3|This is a log"
80+
log.debug('This message will not show');
81+
debugLog.debug('This message will show');
4482
```
4583

46-
## Layout
47-
48-
log4js2 allows for you to format your logs similarly to Log4j2, documented [here](https://logging.apache.org/log4j/2.x/manual/layouts.html). Keep in mind that some of the layout tags are relatively more expensive, and should only really be used in a development environment - such as *%method* and *%line*.
84+
#### tagLayout
85+
Type: `String`
86+
Default: `"%d{HH:mm:ss} [%level] %logger - %message"`
4987

88+
Sets the tagging layout for the logs. Refer to [Apache's Log4j2 documentation](https://logging.apache.org/log4j/2.x/manual/layouts.html) for how to set the tag layout. Keep in mind that some of the layout tags are relatively more expensive, and should only really be used in a development environment - such as *%method* and *%line*.
5089
There are also a few layouts that are not implemented with log4js2:
5190

5291
1. Callers
@@ -73,7 +112,7 @@ log.warn('This is a log {}', 'with parameters');
73112

74113
```
75114

76-
### Note: Showing Method Names
115+
###### Note: Showing Method Names
77116

78117
In order to make the **%method** tag word, you must call from named function, like so:
79118

@@ -91,4 +130,23 @@ var callerFunction = function () {
91130
log.info('This is an anonymous function');
92131
};
93132
// outputs: 03-24-2016 16:19:42,373 [INFO] myLogger.anonymous:3 - This is an anonymous function
94-
```
133+
```
134+
135+
## log4js
136+
137+
#### addAppender(appender)
138+
*appender* `Object`
139+
140+
Adds an appender (see configuration). The configuration option allowAppenderInjection must be set to true for this to work.
141+
142+
#### getLogger(logger)
143+
*logger* `String [optional]`
144+
145+
Gets a logger instance. If the logger is not set, the logger name will be pulled from the containing named instance it was created in (anonymous if unnamed).
146+
147+
#### setLogLevel(logLevel, logger)
148+
149+
*logLevel* `Number` *(use log4js.LogLevel)*
150+
*logger* `String [optional]`
151+
152+
Sets the log level for a specific logger, or all loggers (if logger is not set).

dist/log4js2.js

Lines changed: 18 additions & 26 deletions
Large diffs are not rendered by default.

dist/log4js2.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/log4js2.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "log4js2",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"scripts": {
55
"test": "test/node-test.js"
66
},

src/appenders/consoleAppender.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function ConsoleAppender() {
3737
let message = formatter.format(tagLayout_, loggingEvent);
3838

3939
if (loggingEvent.level == LogLevel.ERROR) {
40-
console.error(message, (loggingEvent.error || null));
40+
console.error(message);
4141
} else if (loggingEvent.level == LogLevel.WARN) {
4242
console.warn(message);
4343
} else if (loggingEvent.level == LogLevel.INFO) {

src/logManager.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ var validateAppender_ = function (appender) {
198198
*
199199
* @param {Object} loggingEvent
200200
*/
201-
export function append(loggingEvent) {
201+
function append(loggingEvent) {
202202

203203
// finalize the configuration to make sure no other appenders are injected (if set)
204204
finalized_ = true;
@@ -237,18 +237,6 @@ var validateLevel_ = function (level) {
237237

238238
};
239239

240-
/**
241-
* Gets the application information from the configuration
242-
* @return {Object}
243-
*/
244-
export function getApplicationInfo() {
245-
if (configuration_.application != null) {
246-
return configuration_.application;
247-
} else {
248-
return null;
249-
}
250-
}
251-
252240
/**
253241
* Handles creating the logger and returning it
254242
* @param {string} context
@@ -268,21 +256,30 @@ export function getLogger(context) {
268256
}
269257

270258
/**
271-
* Sets the log level for all loggers
259+
* Sets the log level for all loggers, or specified logger
272260
* @param {number} logLevel
261+
* @param {string=} logger
273262
*/
274-
export function setLogLevel(logLevel) {
263+
export function setLogLevel(logLevel, logger) {
275264

276265
validateLevel_(logLevel);
277266

278-
for (var logKey in loggers_) {
279-
if (loggers_.hasOwnProperty(logKey)) {
280-
for (var key in loggers_[logKey]) {
281-
if (loggers_[logKey].hasOwnProperty(key)) {
282-
loggers_[logKey][key].setLogLevel(logLevel);
267+
if (logger !== undefined) {
268+
if (loggers_[logger]) {
269+
loggers_[logger].setLogLevel(logLevel);
270+
}
271+
} else {
272+
273+
for (var logKey in loggers_) {
274+
if (loggers_.hasOwnProperty(logKey)) {
275+
for (var key in loggers_[logKey]) {
276+
if (loggers_[logKey].hasOwnProperty(key)) {
277+
loggers_[logKey][key].setLogLevel(logLevel);
278+
}
283279
}
284280
}
285281
}
282+
286283
}
287284

288285
}

test/index.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@
88
<body>
99

1010
<script type="text/javascript">
11+
1112
log4js.configure({
1213
tagLayout : '%d{MM-dd-yyyy HH:mm:ss,S} [%level] %logger:%M:%line|%message',
1314
appenders : [ 'consoleAppender' ],
1415
loggers : [ {
15-
logLevel : log4js.LogLevel.INFO
16+
logLevel : log4js.LogLevel.TRACE
1617
} ],
1718
allowAppenderInjection : true
1819
});
1920

20-
var log = log4js.getLogger();
21+
var log = log4js.getLogger('myLogger');
2122

2223
function namedFunctionLogging() {
2324

test/node-test.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
var log4js = require('../dist/cjs/logManager');
22

33
log4js.configure({
4-
tagLayout : '%d{MM-dd-yyyy HH:mm:ss,S} [%level] %logger:%M:%line|%message',
4+
tagLayout : '%d{MM-dd-yyyy HH:mm:ss,S} [%level] %logger.%M:%line|%message',
55
appenders : [ 'consoleAppender' ],
66
loggers : [ {
77
logLevel : log4js.LogLevel.INFO
8-
} ],
8+
}, {
9+
tag : 'debugLogger',
10+
logLevel : log4js.LogLevel.DEBUG
11+
} ],
912
allowAppenderInjection : true
1013
});
1114

12-
var log = log4js.getLogger();
15+
var log = log4js.getLogger('myLogger');
16+
var debugLog = log4js.getLogger('debugLogger');
1317

1418
function namedFunctionLogging() {
15-
16-
log.trace('This is a named trace message');
17-
log.debug('This is a named debug message');
18-
log.info('This is a named info message');
19-
log.warn('This is a named warn message');
20-
log.error('This is a named error message');
19+
20+
debugLog.trace('This is a named trace message');
21+
debugLog.debug('This is a named debug message');
22+
debugLog.info('This is a named info message');
23+
debugLog.warn('This is a named warn message');
24+
debugLog.error('This is a named error message');
2125

2226
}
2327

0 commit comments

Comments
 (0)