question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

allow winston to read events from piped stream

See original GitHub issue

What’s the feature?

For what I’ve understood after reading winston features, there’s no way using its API to log events from piped streams in a nodejs stream way.

What problem is the feature intended to solve?

Input to winston should be possible to be streams. One use case would be logging child processes stdout and stderr using pipe() instead of using EventEmitters.

Is the absence of this feature blocking you or your team? If so, how?

No, it’s making the code not standard.

Is this feature similar to an existing feature in another tool?

Not sure.

Is this a feature you’re prepared to implement, with support from us?

No.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ChrisAldersoncommented, May 29, 2018

As per winston@3 the Logger is a stream as you can read here, so you can already use .pipe() like this:

'use strict';

const { Transform } = require('stream');
const winston = require('../lib/winston');

//
// Create the logger where we want to pipe
// the out come.
//
const logger = winston.createLogger({
  format: winston.format.simple(),
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({
      filename: './winston.log'
    })
  ]
});

//
// Create a new class which extends from
// `Transform`. This is needed to transform
// each line to an object `winston` can log:
//   - { level: 'info', message: 'foo' }
//
class MyStream extends Transform {
  constructor({ level } = { level: 'info' }) {
    super({
      readableObjectMode: true,
      writableObjectMode: true
    });
   this.level = level;
  }

  _transform(chunk, encoding, callback) {
    this.push({
      level: this.level,
       message: `data: ${chunk}`
    });

    callback();
  }
}

//
// Set the level to 'error', default to
// 'info' in `MyStream`.
//
const myStream = new MyStream({ level: 'error' });

//
// Set the correct encoding so `MyStream`
// can transform it. And pipe it through
// the `Transform` stream to be logged
// with `winston.
//
process.stdin.setEncoding('utf-8');
process.stdin
  .pipe(myStream)
  .pipe(logger);
0reactions
thelazyrkcommented, Nov 12, 2019

This feels like something that is outside of the scope of winston itself since it is so readily accomplished by composing other popular modules in the Node.js ecosystem:

const JSONStream = require('JSONStream');

// Process stdin
process.stdin
  .pipe(JSONStream.parse())
  .pipe(logger);

// Process stderr 
process.stderr
  .pipe(JSONStream.parse())
  .pipe(logger)

If you feel like there is a feature here would love to see a PR, but for now going to leave it open as a documentation issue.

I tried this but it still doesn’t post any of the data to my file.

import * as JSONStream from 'JSONStream'
import * as os from 'os'
import * as path from 'path'
import { createLogger, format, transports } from "winston"
const homeDirectory = os.homedir()
const filename = "file.log"

try { fs.unlinkSync(filename); }
catch (ex) { }
export const fileLogger = createLogger({
    format: format.simple(),
    transports: [
        new transports.File({
            filename
        })
    ]
})

process.stdout.pipe(JSONStream.parse()).pipe(fileLogger)
process.stderr.pipe(JSONStream.parse()).pipe(fileLogger)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I output Connect/Express's logger output to Winston?
Here is what i did to solve this very issue. Basically use the stream option in the connect/express logger module to pipe the...
Read more >
Node Logging Basics - The Ultimate Guide To Logging - Loggly
With Winston, you can do the following: Use multiple transports of the same type; Add custom transports; Stream logs; Query logs; Profiling; Handle...
Read more >
Node.js Logging with Winston - Reflectoring
Logging is used to provide accurate context about what occurs in our application, it is the documentation of all events that happen within ......
Read more >
Data streams | Elasticsearch Guide [8.5] | Elastic
Data streams are well-suited for logs, events, metrics, ... When you submit a read request to a data stream, the stream routes the...
Read more >
PLOT SUMMARY 1984
We are introduced to Winston Smith and the world in which he lives. ... Throughout the book, we also read about Victory gin,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found