Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ logger.error('This town ain\'t big enough for the two of us!');
// error [configured] <Fri, 05 Dec 2014 02:10:48 GMT> This town ain't big enough for the two of us!
```

It's also possible to set a global prefix:
- Configure global prefix by specifying a `FASTLOG_GLOBAL_PREFIX` environment variable.
```javascript
process.env.FASTLOG_GLOBAL_PREFIX = '[ipAddress]';
```

```javascript
var logger = require('fastlog')('configured', 'error', '${level} [${ category }] <${timestamp}>');
logger.error('You\'ve got a friend in me.');
// [ipAddress] error [configured] <Fri, 05 Dec 2014 02:10:48 GMT> You've got a friend in me.
```

## Usage via shell scripts

You may also use fastlog in shell scripts. First, make sure fastlog is installed globally
Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ var util = require('util');
module.exports = function(category, level, template) {
category = category || 'default';
template = template || '[${timestamp}] [${level}] [${category}]';
if (process.env.FASTLOG_GLOBAL_PREFIX) {
template = process.env.FASTLOG_GLOBAL_PREFIX + ' ' + template;
}

var levels = ['debug', 'info', 'warn', 'error', 'fatal'];
return _(levels).reduce(function(logger, l) {
logger[l] = function() {
Expand Down
11 changes: 10 additions & 1 deletion test/fastlog.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ var assert = require('assert');
var sinon = require('sinon');
var fastlog = require('../index.js');

var util = require('util');
var spy = sinon.spy(console, 'log');

describe('string logging', function() {
Expand Down Expand Up @@ -87,4 +86,14 @@ describe('string logging', function() {
assert.ok(spy.calledWith('%s %s', sinon.match(/\[error\] \{configured\} -- .+?:/), 'to infinity and beyond!'));
assert.equal(spy.callCount, 1);
});

it('should include FASTLOG_GLOBAL_PREFIX in prefix', function() {
process.env.FASTLOG_GLOBAL_PREFIX = '[global prefix]';
var log = fastlog();
log.error('reach for the sky!');
assert.ok(
spy.calledWith('%s %s', sinon.match(/\[global prefix\] \[.+\] \[error\] \[default\]/), 'reach for the sky!')
);
delete process.env.FASTLOG_GLOBAL_PREFIX;
});
});