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.

Winston not creating any output when running in a Docker container

See original GitHub issue

Please tell us about your environment:

  • winston version?
    • winston@2
    • winston@3
  • node -v outputs: v14.5.0
  • Operating System? (Windows, macOS, or Linux) macOS + Alpine Linux
  • Language? (all | TypeScript X.X | ES6/7 | ES5 | Dart) ES6/7

What is the problem?

Winston does not print any log output when running inside a Docker container, either to the Console or to a file (it does create the file, just doesn’t put any output there.)

Here is my Winston config.

import winston from 'winston'
import config from '../config'

const transports = []

transports.push(
  new winston.transports.Console(),
  new winston.transports.File({
    filename: 'test.log'
  })
)

const LoggerInstance = winston.createLogger({
  level: config.logs.level,
  levels: winston.config.npm.levels,
  format: winston.format.combine(
    winston.format.timestamp({
      format: 'YYYY-MM-DD HH:mm:ss'
    }),
    winston.format.errors({ stack: true }),
    winston.format.splat(),
    winston.format.json()
  ),
  transports
})

export default LoggerInstance

This is my Dockerfile

FROM node:current-alpine

WORKDIR /usr/src
RUN apk --no-cache add --virtual builds-deps build-base python
COPY package.json package.json
COPY package-lock.json package-lock.json
RUN npm install
COPY . .
ENV NODE_ENV production

CMD ["npm", "run", "start"]

Here is where I’m logging

import expressLoader from './express'
import databaseLoader from './database'
import dependencyLoader from './dependencies'
import modelLoader from './models'
import Logger from './logger'

export default async ({ expressApp }) => {
  const conn = databaseLoader
  Logger.info('✅️ Database loaded')

  try {
    await conn.authenticate()
    Logger.info('🔌 ️Database connected')
  } catch (e) {
    Logger.error('🔥 Error connecting to database: %o', e)
    process.exit(1)
  }

  Logger.info('➕ Injecting dependencies')
  dependencyLoader()
  Logger.info('✅️ Dependencies injected')

  Logger.info('➕ Injecting models')
  modelLoader()
  Logger.info('✅️ Models injected')

  expressLoader({ app: expressApp })
  Logger.info('✅️ Express loaded')
}

I get the following output when running locally

Screenshot 2020-07-09 at 16 28 23

And when I run from my Docker container, I get the following

Screenshot 2020-07-09 at 16 29 05

If I change all of the Logger.info to console.log, I get output.

The log level (and all other config) is the same, and is currently set to silly.

Any ideas?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
indrasenakatamcommented, May 30, 2022

Can you please help me how to write logs into winston file transports using docker container.

using docker able to create logs file and console logs printing in server using docker image.

But the logs are not saved into log files.

So can you please suggest how to write logs into winston file transports using docker image.

WORKDIR /home/app
RUN mkdir -p /path/to/logs && \
    touch /path/to/logs/error.log && \
    touch //path/to/logs/combined.log && \
    chmod 777 /path/to/logs/*

still winston file transport not able to save logs into files. In local environment without docker image it’s working as expected.

Please suggest me how to write & save logs into files using docker image.

0reactions
wbtcommented, Nov 17, 2022

Might be related to #2175 and/or #981/#982

Read more comments on GitHub >

github_iconTop Results From Across the Web

Log files not created in container or host with Winston logger
This behavior is actually discussed at Winston not creating any output when running in a Docker container. The work around mentioned ...
Read more >
node.js - Where is the winston log files when using docker
It's inside the container, in a directory space that's not usually directly accessible from the host. You've also configured Winston to use ...
Read more >
A Complete Guide to Winston Logging in Node.js - Better Stack
Learn how to start logging with Winston in Node.js and go from basics to best practices in no time.
Read more >
How To Use Winston to Log Node.js Applications on Ubuntu ...
By default, applications created with express-generator run on port 3000 , so you need to ensure that the firewall does not block the...
Read more >
Node.js Logging with Winston - Reflectoring
Hence the logger will only output debug and higher levels ( info , warn and error ). Any level lower than debug would...
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