Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for winston 3.x #61

Merged
merged 2 commits into from
Nov 5, 2022
Merged
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
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Changelog

## 1.0.0 (2022-11-02) - Not released yet
## 1.0.0 (2022-11-02)

Features:

- release 1.x branch for for supporting Winston ^2.x
- Add support for winston 3.x

Bugfixes:

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2013 Jaakko Suutarla
Copyright (c) 2022 Jaakko Suutarla

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
55 changes: 30 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,46 @@

A [Logstash TCP][0] transport for [winston][1].

## OBS
## Usage

Due changes to Winston version >= 1.0.0 supports winston < 3.x. Updated version with Winston 3.x support will be released soon.
### Winston 2.x

## Usage
``` js
// See test cases from test-bench/winston-2x/test/smoke.js
const winston = require('winston');
const transports = require('winston-logstash');

const logger = new (winston.Logger)({
transports: [
new transports.Logstash({
port: 28777,
node_name: 'my node name',
host: '127.0.0.1'})]});

log.info("Hello world!");
```

### Node
### Winston 2.x

``` js
var winston = require('winston');

//
// Requiring `winston-logstash` will expose
// `winston.transports.Logstash`
//
require('winston-logstash');

winston.add(winston.transports.Logstash, {
port: 28777,
node_name: 'my node name',
host: '127.0.0.1'
});
// See test cases from test-bench/winston-3x/test/smoke.js
const winston = require('winston');
const LogstashTransport = require('winston-logstash/lib/winston-logstash-latest');

const logger = winston.createLogger({
transports: [
new LogstashTransport({
port: 28777,
node_name: 'my node name',
host: '127.0.0.1'})]});

log.info("Hello world!");
```

### Logstash config

``` ruby
# See example from test-bench/logstash/logstash/pipeline/default.conf
input {
# Sample input over TCP
tcp { port => 28777 type=>"sample" }
Expand All @@ -56,14 +69,6 @@ Due changes to Winston version >= 1.0.0 supports winston < 3.x. Updated version
npm test
```

## TODO

1. Rethink logstash integration ( https://github.com/flatiron/winston/blob/master/lib/winston/common.js#L149 )
2. Rewrite
3. Release major after rewrite

N. Clean up tests ( refactor )

#### Author: [Jaakko Suutarla](https://github.com/jaakkos)

#### License: MIT
Expand Down
35 changes: 35 additions & 0 deletions lib/winston-logstash-latest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* eslint-disable require-jsdoc */
const Transport = require('winston-transport');
const Manager = require('./manager');

//
// Inherit from `winston-transport` so you can take advantage
// of the base functionality and `.exceptions.handle()`.
//
module.exports = class LogstashTransport extends Transport {
constructor(options) {
super(options);

this.name = 'logstash';

this.manager = new Manager(options, this.onError.bind(this));
this.manager.start();
}

onError(error) {
this.silent = true;
this.emit('error', error);
}

log(info, callback) {
setImmediate(() => {
this.emit('logged', info);
});

// Perform the writing to the remote service

this.manager.log(JSON.stringify(info), () => {
callback();
});
}
};
4 changes: 1 addition & 3 deletions lib/winston-logstash.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable require-jsdoc */
/*
*
* (C) 2013 Jaakko Suutarla
* (C) 2022 Jaakko Suutarla
* MIT LICENCE
*
*/
Expand All @@ -18,8 +18,6 @@ class Logstash extends winston.Transport {

this.name = 'logstash';
this.localhost = options.localhost || os.hostname();
this.host = options.host || '127.0.0.1';
this.port = options.port || 28777;
this.node_name = options.node_name || process.title;
this.pid = options.pid || process.pid;

Expand Down
Loading