Skip to content

Commit

Permalink
Move to 0.1.1
Browse files Browse the repository at this point in the history
Updated documentation and progressed version number
  • Loading branch information
anigenero committed Mar 28, 2016
1 parent 6cf7f45 commit 2ed380c
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 75 deletions.
84 changes: 71 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,31 @@ A fast, lightweight (~12KB compressed) JavaScript logger with no runtime depende

## Setup

Simply require the log4js
Simply require the log4js2 module.

```javascript
var log4js = require('log4js2');
```

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

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

Then, log some stuff!!

```javascript

// create the logger
var log = log4js.getLogger('myLogger');

// log an event
log.info('This is a log');

// output: "03-24-2016 12:00:18,670|myLogger:anonymous:3|This is a log"
```

## Configuration

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
Expand All @@ -30,23 +43,49 @@ log4js.configure({
});
```

Log some stuff
### Configuration Options

#### allowAppenderInjection
Type: `Boolean`
Default: `false`

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.

#### appenders
Type: `Array.<String>`
Default: `[ 'consoleAppender' ]`

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.

#### loggers
Type: `Array.<Object>`
Default: `[{ tag : 'main', logLevel : log4js.LogLevel.INFO }]`

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`).

```javascript
log4js.configure({
// ...
loggers : [ {
logLevel : log4js.LogLevel.INFO
}, {
tag : 'debugLogger',
logLevel : log4js.LogLevel.DEBUG
} ]
});

// create the logger
var log = log4js.getLogger('myLogger');
var debugLog = log4js.getLogger('debugLogger');

// log an event
log.info('This is a log');

// output: "03-24-2016 12:00:18,670|myLogger:anonymous:3|This is a log"
log.debug('This message will not show');
debugLog.debug('This message will show');
```

## Layout

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*.
#### tagLayout
Type: `String`
Default: `"%d{HH:mm:ss} [%level] %logger - %message"`

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*.
There are also a few layouts that are not implemented with log4js2:

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

```

### Note: Showing Method Names
###### Note: Showing Method Names

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

Expand All @@ -91,4 +130,23 @@ var callerFunction = function () {
log.info('This is an anonymous function');
};
// outputs: 03-24-2016 16:19:42,373 [INFO] myLogger.anonymous:3 - This is an anonymous function
```
```

## log4js

#### addAppender(appender)
*appender* `Object`

Adds an appender (see configuration). The configuration option allowAppenderInjection must be set to true for this to work.

#### getLogger(logger)
*logger* `String [optional]`

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).

#### setLogLevel(logLevel, logger)

*logLevel* `Number` *(use log4js.LogLevel)*
*logger* `String [optional]`

Sets the log level for a specific logger, or all loggers (if logger is not set).
44 changes: 18 additions & 26 deletions dist/log4js2.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/log4js2.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/log4js2.min.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "log4js2",
"version": "0.1.0",
"version": "0.1.1",
"scripts": {
"test": "test/node-test.js"
},
Expand Down
2 changes: 1 addition & 1 deletion src/appenders/consoleAppender.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function ConsoleAppender() {
let message = formatter.format(tagLayout_, loggingEvent);

if (loggingEvent.level == LogLevel.ERROR) {
console.error(message, (loggingEvent.error || null));
console.error(message);
} else if (loggingEvent.level == LogLevel.WARN) {
console.warn(message);
} else if (loggingEvent.level == LogLevel.INFO) {
Expand Down
37 changes: 17 additions & 20 deletions src/logManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ var validateAppender_ = function (appender) {
*
* @param {Object} loggingEvent
*/
export function append(loggingEvent) {
function append(loggingEvent) {

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

};

/**
* Gets the application information from the configuration
* @return {Object}
*/
export function getApplicationInfo() {
if (configuration_.application != null) {
return configuration_.application;
} else {
return null;
}
}

/**
* Handles creating the logger and returning it
* @param {string} context
Expand All @@ -268,21 +256,30 @@ export function getLogger(context) {
}

/**
* Sets the log level for all loggers
* Sets the log level for all loggers, or specified logger
* @param {number} logLevel
* @param {string=} logger
*/
export function setLogLevel(logLevel) {
export function setLogLevel(logLevel, logger) {

validateLevel_(logLevel);

for (var logKey in loggers_) {
if (loggers_.hasOwnProperty(logKey)) {
for (var key in loggers_[logKey]) {
if (loggers_[logKey].hasOwnProperty(key)) {
loggers_[logKey][key].setLogLevel(logLevel);
if (logger !== undefined) {
if (loggers_[logger]) {
loggers_[logger].setLogLevel(logLevel);
}
} else {

for (var logKey in loggers_) {
if (loggers_.hasOwnProperty(logKey)) {
for (var key in loggers_[logKey]) {
if (loggers_[logKey].hasOwnProperty(key)) {
loggers_[logKey][key].setLogLevel(logLevel);
}
}
}
}

}

}
Expand Down
5 changes: 3 additions & 2 deletions test/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
<body>

<script type="text/javascript">

log4js.configure({
tagLayout : '%d{MM-dd-yyyy HH:mm:ss,S} [%level] %logger:%M:%line|%message',
appenders : [ 'consoleAppender' ],
loggers : [ {
logLevel : log4js.LogLevel.INFO
logLevel : log4js.LogLevel.TRACE
} ],
allowAppenderInjection : true
});

var log = log4js.getLogger();
var log = log4js.getLogger('myLogger');

function namedFunctionLogging() {

Expand Down
22 changes: 13 additions & 9 deletions test/node-test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
var log4js = require('../dist/cjs/logManager');

log4js.configure({
tagLayout : '%d{MM-dd-yyyy HH:mm:ss,S} [%level] %logger:%M:%line|%message',
tagLayout : '%d{MM-dd-yyyy HH:mm:ss,S} [%level] %logger.%M:%line|%message',
appenders : [ 'consoleAppender' ],
loggers : [ {
logLevel : log4js.LogLevel.INFO
} ],
}, {
tag : 'debugLogger',
logLevel : log4js.LogLevel.DEBUG
} ],
allowAppenderInjection : true
});

var log = log4js.getLogger();
var log = log4js.getLogger('myLogger');
var debugLog = log4js.getLogger('debugLogger');

function namedFunctionLogging() {
log.trace('This is a named trace message');
log.debug('This is a named debug message');
log.info('This is a named info message');
log.warn('This is a named warn message');
log.error('This is a named error message');

debugLog.trace('This is a named trace message');
debugLog.debug('This is a named debug message');
debugLog.info('This is a named info message');
debugLog.warn('This is a named warn message');
debugLog.error('This is a named error message');

}

Expand Down

0 comments on commit 2ed380c

Please sign in to comment.