Skip to content

Commit

Permalink
Merge pull request #18 from JoshRagem/master
Browse files Browse the repository at this point in the history
update to Winston 3.1
  • Loading branch information
megastef authored Oct 17, 2018
2 parents 3a02029 + a936513 commit adb11e9
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 49 deletions.
38 changes: 20 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![](https://travis-ci.org/sematext/winston-logsene.svg?branch=master)
![](https://travis-ci.org/sematext/winston-logsene.svg?branch=master)


This is a transport module for the winston logger [winstonjs/winston](https://github.com/winstonjs/winston) for logging with [Logsene](http://www.sematext.com/logsene) by Sematext.
Expand All @@ -11,24 +11,25 @@ Create your free account and access token [here](https://apps.sematext.com/users
```js

var winston = require('winston')
var logsene = require('winston-logsene')
var logger = new winston.Logger()
logger.add (logsene, {
token: process.env.LOGSENE_TOKEN,
type: 'test_logs',
url: 'https://logsene-receiver.sematext.com/_bulk'
var Logsene = require('winston-logsene')
var logger = winston.createLogger({
transports: [new Logsene({
token: process.env.LOGSENE_TOKEN,
type: 'test_logs',
url: 'https://logsene-receiver.sematext.com/_bulk'
})]
})
```
### Options

- __token__ - Create your free account and access token [here](https://apps.sematext.com/users-web/register.do).
- __type__ - Type of your logs - please note you can define [Elasticsearch mapping templates in Logsene](http://blog.sematext.com/2015/02/09/elasticsearch-mapping-types-for-json-logging/)
- __type__ - Type of your logs - please note you can define [Elasticsearch mapping templates in Logsene](http://blog.sematext.com/2015/02/09/elasticsearch-mapping-types-for-json-logging/)
- __url__ - Logsene receiver URL (e.g. for Logsene On Premises), defaults to Sematext Cloud (US) receiver `https://logsene-receiver.sematext.com/_bulk`. To ship logs to Sematext Cloud (EU) in Europe use `https://logsene-receiver.eu.sematext.com/_bulk`
- __handleExceptions__ - boolean 'true' logs 'uncaught exceptions'
- __exitOnError__ - if set to 'false' process will not exit after logging the 'uncaught exceptions'
- __source__ - name of the logging source, by default name of the main node.js module
- __setSource__ - "true" adds "source" to the log event (modifies the object!), default false
- __rewriter__ - similar to rewriters in winston, rewriter allows modifying of __log meta__ but only for the logsene
- __rewriter__ - similar to rewriters in winston, rewriter allows modifying of __log meta__ but only for the logsene
transport. This is a simple function which takes `level, msg, meta` as parameter and returns the new __meta__ array
- __flushOnExit__ - Handling SIGTERM, SIGQUIT, SIGINT and 'beforeExit' to flush logs and terminate. Default value: true.

Expand All @@ -48,37 +49,38 @@ logger.debug ("Debug message no. %d logged to %s",1,'Logsene', {metadata: "test-

// use custom rewriter
var serverIp = "10.0.0.12";
logger.add (logsene, {
var logger = winston.createLogger({
transports: [new Logsene({
token: process.env.LOGSENE_TOKEN,
rewriter: function (level, msg, meta) {
meta.ip = serverIp;
return meta;
}
})]
})

```

### Schema / Mapping definition for Meta-Data

It is possible to log any JSON Object as meta data, but please note Logsene stores data in Elasticsearch and therefore you should define an index template, matching your data structure.
More about Elasticsearch mapping and templates for Logsene:
It is possible to log any JSON Object as meta data, but please note Logsene stores data in Elasticsearch and therefore you should define an index template, matching your data structure.
More about Elasticsearch mapping and templates for Logsene:
[http://blog.sematext.com/2015/01/20/custom-elasticsearch-index-templates-in-logsene/](http://blog.sematext.com/2015/01/20/custom-elasticsearch-index-templates-in-logsene/)

In addition you should use different types for different meta data structures to avoid type conflicts in Elasticsearch. Include a type name in the meta-data like {type: 'logType1', ...} - this overwrites the "type" property, specified in the contstructor.
In addition you should use different types for different meta data structures to avoid type conflicts in Elasticsearch. Include a type name in the meta-data like {type: 'logType1', ...} - this overwrites the "type" property, specified in the contstructor.
```
logger.add (logsene, {token: process.env.LOGSENE_TOKEN, type: 'my_logs'})
// numeric id, log type from constructor
logger.info('hello', {id: 1})
// The next line will cause a type conflict in Elasticsearch/Logsene, because id was a number before
logger.info('hello', {id: 'ID-1'})
logger.info('hello', {id: 'ID-1'})
// using a different type, OK no type conflict for the field 'id' in Elasticsearch/Logsene
// because we use a different type in the Elasticsearch/Logsene index
// because we use a different type in the Elasticsearch/Logsene index
logger.info('hello', {type: 'my_type_with_string_ids',{id: 'ID-1'})
```

## Security

- HTTPS is enabled by default
- HTTPS is enabled by default
- Environment variables for Proxy servers:
- For HTTPS endpoints (default): HTTPS_PROXY / https_proxy
```
Expand All @@ -90,7 +92,7 @@ logger.info('hello', {type: 'my_type_with_string_ids',{id: 'ID-1'})
export HTTP_PROXY=http://my-proxy.example
export HTTP_PROXY=https://my-ssl-proxy.example
```

## License

Apache 2, see LICENSE file
Expand Down
9 changes: 5 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict'
var util = require('util')
var winston = require('winston')
var Transport = winston.Transport
var Transport = require('winston-transport')
var common = require('winston/lib/winston/common')
var _dirname = require('path').dirname
var LogseneJS = require('logsene-js')
Expand Down Expand Up @@ -118,7 +117,7 @@ var Logsene = function (options) {
//
// Inherit from `winston.Transport`.
//
util.inherits(Logsene, winston.Transport)
util.inherits(Logsene, Transport)

//
// Expose the name of this Transport on the prototype
Expand All @@ -133,7 +132,9 @@ Logsene.prototype.name = 'Logsene'
// #### @callback {function} Continuation to respond to when complete.
// Core logging method exposed to Winston. Metadata is optional.
//
Logsene.prototype.log = function (level, msg, meta, callback) {
Logsene.prototype.log = function (meta, callback) {
var level = meta.level
var msg = meta.message
this.logCount++
var metaData = meta
if (this.silent) {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"dependencies": {
"flat": "^2.0.0",
"logsene-js": "^1.1.49",
"winston": "^2.3.1"
"winston": "^3.1.0",
"winston-transport": "^4.2.0"
},
"devDependencies": {
"mocha": "^2.5.3"
Expand Down
65 changes: 39 additions & 26 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@ describe('Logsene log ', function () {
try {
var winston = require('winston')
var Logsene = require('../lib/index.js')
var logger = new winston.Logger()
logger.add(Logsene, {token: process.env.LOGSENE_TOKEN})
var counter = 0
var transport = new Logsene({token: process.env.LOGSENE_TOKEN})
var logger = winston.createLogger({
format: winston.format.combine(winston.format.splat(), winston.format.simple()),
transports:[transport]
})
for (var i = 0; i < 100; i++) {
logger.info('Test %d for %s', i, 'logsene', {x: i, y: {arr: [1, 2, 3, 4]}}, function (err, res) {
counter++
if (counter == 99) {
done()
}
if (err) {
done(err)
}
})
logger.info('Test %d for %s', i, 'logsene', {x: i, y: {arr: [1, 2, 3, 4]}})
}
transport.on('logged', function (data) {
if (data.count == 100) {
done()
}
})
transport.on('finish', function (err) {
if (err) {
done(err)
}
})
logger.end()
} catch (ex) {
console.log(ex.stack)
done(ex)
Expand All @@ -29,18 +34,22 @@ describe('Logsene log source', function () {
try {
var winston = require('winston')
var Logsene = require('../lib/index.js')
var logger = new winston.Logger()
logger.add(Logsene, {token: process.env.LOGSENE_TOKEN, setSource: true, source: 'mocha-test'})
logger.info('Test', function (err, level, message, data) {
var transport = new Logsene({token: process.env.LOGSENE_TOKEN, setSource: true, source: 'mocha-test'})
var logger = winston.createLogger({
transports:[transport]
})
transport.logger.on('logged', function(data) {
if (data.msg.source === 'mocha-test') {
done()
}
})
logger.info('Test')
logger.on('finish', function (err, level, message, data) {
if (err) {
done(err)
}
if (data.source === 'mocha-test') {
done()
} else {
done(new Error('source not correct:' + data.source))
}
})
logger.end()
} catch (ex) {
console.log(ex.stack)
done(ex)
Expand All @@ -52,9 +61,7 @@ describe('Logsene rewrite hook', function () {
try {
var winston = require('winston')
var Logsene = require('../lib/index.js')
var logger = new winston.Logger()
var serverIp = '10.0.0.12'
logger.add(Logsene, {
var transport = new Logsene({
token: process.env.LOGSENE_TOKEN,
setSource: false,
flushOnExit: true,
Expand All @@ -63,14 +70,20 @@ describe('Logsene rewrite hook', function () {
return meta
}
})
var logger = winston.createLogger({
transports:[transport]
})
var serverIp = '10.0.0.12'
var lo = {x: 1}
logger.info('Test', lo, function (err, level, message, meta) {
if (meta.ip === serverIp) {
transport.logger.on('logged', function(data) {
if (data.msg.ip === serverIp) {
done()
} else {
done(new Error('rewrite field ip is not correct:' + meta.ip))
done(new Error('rewrite field ip is not correct:' + data.ip))
}
})
logger.info('Test', lo)
logger.end()
} catch (ex) {
console.log(ex.stack)
done(ex)
Expand Down

0 comments on commit adb11e9

Please sign in to comment.