ft-next-logger
Advanced tools
Comparing version 2.2.3 to 3.0.0
@@ -1,2 +0,1 @@ | ||
/* global process */ | ||
'use strict'; | ||
@@ -3,0 +2,0 @@ |
@@ -1,2 +0,1 @@ | ||
/* global process, __dirname */ | ||
'use strict'; | ||
@@ -14,4 +13,9 @@ | ||
this.name = 'splunk'; | ||
this.appName = opts.appName; | ||
this.agent = (opts && opts.agent) || fork(path.resolve(__dirname, '..', 'agent.js'), [`${process.env.SPLUNK_URL}${this.appName}`]); | ||
const url = `${opts.splunkUrl}${opts.appName}`; | ||
if (opts && opts.agent) { | ||
this.agent = opts.agent; | ||
this.agent.url = url; | ||
} else { | ||
this.agent = fork(path.resolve(__dirname, '..', 'agent.js'), [url]); | ||
} | ||
} | ||
@@ -18,0 +22,0 @@ |
57
main.js
@@ -1,2 +0,1 @@ | ||
/* global process */ | ||
'use strict'; | ||
@@ -14,31 +13,53 @@ | ||
init(appName, opts, transports) { | ||
if (this.inited) { | ||
addConsole(level, opts) { | ||
if (this.logger.transports.console) { | ||
return; | ||
} | ||
this.inited = true; | ||
this.appName = appName; | ||
this.opts = opts; | ||
// add console logger | ||
this.logger.add( | ||
winston.transports.Console, | ||
Object.assign({}, { | ||
level: process.env.NODE_ENV === 'test' ? 'error' : 'info' | ||
level: level || 'info' | ||
}, opts) | ||
); | ||
if (process.env.NODE_ENV === 'production' || (transports && transports.splunk)) { | ||
// add splunk logger | ||
this.logger.add( | ||
Splunk, | ||
Object.assign({}, { | ||
level: 'error', | ||
appName | ||
}, opts) | ||
); | ||
} | ||
removeConsole() { | ||
if (!this.logger.transports.console) { | ||
return; | ||
} | ||
this.logger.remove('console'); | ||
} | ||
addSplunk(appName, splunkUrl, level, opts) { | ||
if (this.logger.transports.splunk) { | ||
return; | ||
} | ||
if (!appName || !splunkUrl) { | ||
this.logger.warn('No `appName` or `splunkUrl` supplied'); | ||
return false; | ||
} | ||
this.logger.add( | ||
Splunk, | ||
Object.assign({}, { | ||
level: level || 'error', | ||
appName, | ||
splunkUrl | ||
}, opts) | ||
); | ||
} | ||
removeSplunk() { | ||
if (!this.logger.transports.splunk) { | ||
return; | ||
} | ||
this.logger.remove('splunk'); | ||
} | ||
clearLoggers() { | ||
Object.keys(this.logger.transports) | ||
.forEach(logger => this.logger.remove(logger)); | ||
} | ||
} | ||
module.exports = new Logger(); |
{ | ||
"name": "ft-next-logger", | ||
"version": "2.2.3", | ||
"version": "3.0.0", | ||
"main": "main.js", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -8,4 +8,4 @@ # Next Logger [![Circle CI](https://circleci.com/gh/Financial-Times/next-logger.svg?style=svg)](https://circleci.com/gh/Financial-Times/next-logger) | ||
``` | ||
# init first, setting the app's name | ||
require('ft-next-logger').init('ft-next-front-page'); | ||
# add a logger | ||
require('ft-next-logger').addConsole(); | ||
@@ -21,2 +21,12 @@ # then.. | ||
### Loggers | ||
#### Console | ||
`addConsole(level = 'info', opts = {})` | ||
#### Splunk | ||
`addSplunk(appName, splunkUrl, level = 'error', opts = {})` | ||
## Releasing | ||
@@ -23,0 +33,0 @@ |
@@ -1,2 +0,1 @@ | ||
/* global __dirname, describe, beforeEach, afterEach, it */ | ||
'use strict'; | ||
@@ -3,0 +2,0 @@ |
@@ -1,2 +0,1 @@ | ||
/* global describe, it */ | ||
'use strict'; | ||
@@ -3,0 +2,0 @@ |
@@ -1,2 +0,1 @@ | ||
/* global describe, it */ | ||
'use strict'; | ||
@@ -15,3 +14,3 @@ | ||
it('should be able to instantiate', () => { | ||
const splunkTransport = new Splunk({ appName: 'ft-next-front-page' }); | ||
const splunkTransport = new Splunk({ splunkUrl: 'http://splunk.ft.com/', appName: 'ft-next-front-page' }); | ||
splunkTransport.should.exist; | ||
@@ -25,3 +24,4 @@ splunkTransport.agent.kill(); | ||
}; | ||
const splunkTransport = new Splunk({ appName: 'ft-next-front-page', agent: mockAgent }); | ||
const splunkTransport = new Splunk({ splunkUrl: 'http://splunk.ft.com/', appName: 'ft-next-front-page', agent: mockAgent }); | ||
mockAgent.url = 'http://splunk.ft.com/ft-next-front-page'; | ||
splunkTransport.log('error', 'a message', { field: 'value'}); | ||
@@ -28,0 +28,0 @@ mockAgent.send.called.should.be.true; |
@@ -1,2 +0,1 @@ | ||
/* global describe, before, it */ | ||
'use strict'; | ||
@@ -9,4 +8,4 @@ | ||
before(() => { | ||
logger.init('ft-next-front-page', {}, { splunk: true }); | ||
afterEach(() => { | ||
logger.clearLoggers(); | ||
}); | ||
@@ -18,16 +17,74 @@ | ||
it('should handle init-ing twice', () => { | ||
logger.init('ft-next-front-page'); | ||
it('should be able to clear loggers', () => { | ||
logger.addConsole(); | ||
logger.clearLoggers(); | ||
logger.logger.transports.should.be.empty; | ||
}); | ||
it('should have console and splunk transports', () => { | ||
const transports = logger.logger.transports; | ||
transports.console.should.exists; | ||
transports.splunk.should.exists; | ||
describe('Console', () => { | ||
it('should be able to add', () => { | ||
logger.addConsole('warn'); | ||
logger.logger.transports.console.level.should.equal('warn'); | ||
}); | ||
it('should have "info" level by default', () => { | ||
logger.addConsole(); | ||
logger.logger.transports.console.level.should.equal('info'); | ||
}); | ||
it('should not be able to add if already added', () => { | ||
logger.addConsole(); | ||
(() => logger.addConsole()).should.not.throw(Error); | ||
}); | ||
it('should be able to remove', () => { | ||
logger.addConsole(); | ||
logger.removeConsole(); | ||
logger.logger.transports.should.be.empty; | ||
}); | ||
it('should not be able to remove if not added', () => { | ||
(() => logger.removeConsole()).should.not.throw(Error); | ||
}); | ||
}); | ||
it('should log to splunk on error', () => { | ||
logger.logger.transports.splunk.level.should.equal('error'); | ||
describe('Splunk', () => { | ||
it('should be able to add', () => { | ||
logger.addSplunk('http://splunk.ft.com', 'ft-next-front-page', 'warn'); | ||
logger.logger.transports.splunk.level.should.equal('warn'); | ||
}); | ||
it('should have "error" level by default', () => { | ||
logger.addSplunk('http://splunk.ft.com', 'ft-next-front-page'); | ||
logger.logger.transports.splunk.level.should.equal('error'); | ||
}); | ||
it('should return false if no `host` supplied', () => { | ||
logger.addSplunk().should.equal(false); | ||
}); | ||
it('should return false if no `appName` supplied', () => { | ||
logger.addSplunk('http://splunk.ft.com').should.equal(false); | ||
}); | ||
it('should not be able to add if already added', () => { | ||
logger.addSplunk(); | ||
(() => logger.addSplunk()).should.not.throw(Error); | ||
}); | ||
it('should be able to remove', () => { | ||
logger.addSplunk(); | ||
logger.removeSplunk(); | ||
logger.logger.transports.should.be.empty; | ||
}); | ||
it('should not be able to remove if not added', () => { | ||
(() => logger.removeSplunk()).should.not.throw(Error); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
9692
277
36
0