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.

Cannot modify logger format in TypeScript

See original GitHub issue

Please tell us about your environment:

  • winston version?
    • winston@2
    • winston@3
  • node -v outputs: 8.9.4
  • Operating System? macOS and Linux
  • Language? TypeScript 2.7.2

What is the problem?

I want to be able to modify the default Logger of winston in my own log module and access that same logger from all other modules in my project. This worked just fine in Node, but I cannot modify the format in TypeScript.

When I try to customize the format, I receive the error

error 2339: Property ‘format’ does not exist on type ‘Winston’.

export const logger = new winston.Logger({
    level: 'debug',
    // same error on the next 3 lines
    format: winston.format.combine(
        winston.format.timestamp(),
        winston.format.json()
    ),
    transports: [
        new winston.transports.File({ filename: 'server_error.log', level: 'error' }),
        new winston.transports.File({ filename: 'server_all.log' })
    ]
});

What do you expect to happen instead?

This was the code I used in Node:

const winston = require('winston');

const logger = winston.createLogger({
	level: 'debug', // Levels: error: 0, warn: 1, info: 2, verbose: 3, debug: 4, silly: 5 
  	format: winston.format.combine(
		winston.format.timestamp(),
		winston.format.json()
	),
  	transports: [
    	new winston.transports.File({ filename: 'server_error.log', level: 'error'}),
    	new winston.transports.File({ filename: 'server_all.log'})
  	]
});

// If we're not in production then log to the `console` with the format:
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
if (process.env.NODE_ENV !== 'production') {
  logger.add(new winston.transports.Console({
    format: winston.format.simple()
  }));
}

exports.logger = logger;

I expect the same format customization to work in TypeScript as well.

Other information

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
algvcommented, May 16, 2018

Example:

import * as winston from "winston";
import { APP_LOG_FILE } from "./constants";

const logger = winston.createLogger({
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.json(),
  ),
  level: "debug",
  transports: [
    new winston.transports.File({
      filename: APP_LOG_FILE,
      maxsize: 4096,
    }),
  ],
});

if (process.env.NODE_ENV !== "production") {
  logger.add(new winston.transports.Console({
    format: winston.format.simple(),
  }));
}

export default logger;
0reactions
davidpasztorcommented, May 17, 2018

Thanks, for the moment I added all 3 .d.ts files manually, which works fine and will update to RC6 as soon as its out!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot format code on visual studio when using TypeScript
The problem was that I haven't installed any Visual Studio features that support javascript & typescript. The solution is press "Tools" ...
Read more >
typescript-logging - npm
TypeScript Logging core written in and to be used by TypeScript (this is the core project, you need to install a flavor too)....
Read more >
Documentation - Module Resolution - TypeScript
Module resolution is the process the compiler uses to figure out what an import refers to. Consider an import statement like import {...
Read more >
tslog - Extensible TypeScript Logger for Node.js and Browser ...
Extensible TypeScript Logger for Node.js and Browser: Dependency free, Fully customizable, Pretty errors, stack traces, and JSON output to attachable ...
Read more >
TypeScript debugging with Visual Studio Code
json and the VS Code Node.js debugger can't map your TypeScript source code to the running JavaScript. Turn on source maps and rebuild...
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