How to use the transports function from winston

Find comprehensive JavaScript winston.transports code examples handpicked from public code repositorys.

winston.transports is a module in the Winston logging library that provides various built-in transports for outputting log messages to different destinations.

112
113
114
115
116
117
118
119
120
121
if (isGoogleCloudEnv === 'true') {
  // Running on Google Cloud, stream logs to Stackdriver.
  mainLogTransport = new LoggingWinston({prefix: label});
} else {
  // Running locally, stream logs to stdout.
  mainLogTransport = new winston.transports.Console({
    format: winston.format.combine(
        upperCaseLevel(),
        winston.format.timestamp(),
        winston.format.colorize(),
fork icon72
star icon140
watch icon33

+ 2 other calls in file

36
37
38
39
40
41
42
43
44
45
// Winston logger configuration
const logger = winston.createLogger({
    level: 'info',
    transports: [
        // Configures the log file
        new winston.transports.File({
            filename: 'list-nmos-server.log',
            maxsize: constants.MAX_LOG_SIZE_10_MB,
            maxFiles: 1,
            tailable: true,
fork icon25
star icon84
watch icon18

+ 15 other calls in file

How does winston.transports work?

In Winston, a transport is an object that defines how log messages are handled or stored. The winston.transports module provides several built-in transports that you can use to send log messages to different locations, such as the console, files, databases, or even third-party services. Each transport defines a log method that takes a log message and associated metadata, and then sends it to the configured destination. To use a transport in Winston, you typically create a new instance of the transport and then add it to your Winston logger instance. For example, to use the console transport, you can do: javascript Copy code {{{{{{{ const winston = require('winston'); const logger = winston.createLogger({ transports: [ new winston.transports.Console() ] }); logger.info('Hello, Winston!'); In this example, we create a new Winston logger instance and configure it to use the built-in Console transport. We then log an info message using the logger.info method, which outputs the message to the console. You can also configure the transports to use custom settings, such as logging levels, file paths, or database connections. Additionally, you can create your own custom transports by extending the winston.Transport class and defining your own log method. Overall, the winston.transports module provides a flexible and extensible way to route log messages to different destinations in your application.

15
16
17
18
19
20
21
22
23
24
  transports: [
    new winston.transports.Console({
      level: 'debug',
      format: winston.format.simple()
    }),
    new winston.transports.File({ filename: 'error.log', level: 'error' })
  ]
})

const MAX_PART_SIZE = bytes('5 MB')
fork icon5
star icon11
watch icon6

+ 7 other calls in file

126
127
128
129
130
131
132
133
134
135
136
);


// Winston instance
const logger = (module.exports = winston.createLogger({
    level: logLevel,
    transports: [new winston.transports.Console()],
    format: winston.format.combine(
        winston.format.timestamp(),
        winston.format.colorize({
            all: true,
fork icon1
star icon8
watch icon2

+ 7 other calls in file

Ai Example

1
2
3
4
5
6
7
const winston = require("winston");

const logger = winston.createLogger({
  transports: [new winston.transports.File({ filename: "logs/app.log" })],
});

logger.info("Hello, Winston!");

In this example, we create a new Winston logger instance and configure it to use the File transport. We set the filename option to logs/app.log, which specifies the file path to write the log messages to. Then, we log an info message using the logger.info method, which outputs the message to the file logs/app.log. You can also specify additional options for the File transport, such as the logging level, file rotation, or file permissions. For example: javascript Copy code

54
55
56
57
58
59
60
61
62
63
        })
    ),
    defaultMeta: { thread: process.pid.toString().padStart(14, '0') } // to fit native format
});
if (LOG_APPENDER !== 'console') {
    logger.add(new winston.transports.File({
        filename: `${LOG_FILE_PATH}/docserver-demo-error.log`,
        level: 'error'
    }));
    logger.add(new winston.transports.File({ filename: LOG_FILE_PATH + '/docserver-demo-technical.log' }));
fork icon4
star icon4
watch icon4

+ 56 other calls in file

30
31
32
33
34
35
36
37
38
39
40
    timestamp: true,
    level: 'info',
    format: winston.format.json(),
    defaultMeta: {service: 'databaseInfoCollection'},
    transports: [
        new winston.transports.File({filename: __dirname + '/combined.json'})
    ],
});


const {performance} = require('perf_hooks');
fork icon2
star icon3
watch icon1

+ 14 other calls in file

203
204
205
206
207
208
209
210
211
212
213
    timestamp: true,
    level: 'info',
    format: winston.format.json(),
    defaultMeta: {service: 'express_information'},
    transports: [
        new winston.transports.File({filename: __dirname + '/public/scripts/combined.json'})
    ],
});


const cache = (durationMs) => {
fork icon2
star icon3
watch icon1

+ 14 other calls in file

34
35
36
37
38
39
40
41
42
transports: [
    //
    // - Write all logs with level `error` and below to `error.log`
    // - Write all logs with level `info` and below to `combined.log`
    //
    // new winston.transports.File({ filename: 'snippets-explorer-error.log', level: 'error' }),
    // new winston.transports.File({ filename: '/tmp/snippets-explorer-combined.log' }),

    new winston.transports.File({ filename: mainLogFileFullPath }),
fork icon1
star icon2
watch icon0

+ 3 other calls in file

176
177
178
179
180
181
182
183
184
185
      new winston.transports.File({
        filename: 'eltern-emailer.log',
        maxsize: 10 << 20,
        maxFiles: 2
      }),
      new winston.transports.Console()
    ]
  });
}

fork icon1
star icon1
watch icon1

+ 37 other calls in file

63
64
65
66
67
68
69
70
71
72
  filename: './logs/bosch-app-combined-%DATE%.log',
  datePattern: 'YYYY-MM-DD',
  zippedArchive: false,
  maxSize: '20m'
});
var protocolsLogs = new winston.transports.DailyRotateFile({
  filename: './logs/bosch-app-protocols-%DATE%.log',
  datePattern: 'YYYY-MM-DD',
  zippedArchive: false,
  maxSize: '20m'
fork icon1
star icon1
watch icon1

+ 74 other calls in file

84
85
86
87
88
89
90
91
92
93
94
95


const WinstonLogCreator = (logLevel, fileName) => {
    const logger = winston.createLogger({
        level: KafkatoWinstonLogLevel(logLevel),
        transports: [
            new winston.transports.File({ filename: fileName })
        ]
    })


    return ({ namespace, level, label, log }) => {
fork icon4
star icon0
watch icon2

+ 8 other calls in file

1
2
3
4
5
6
7
8
9
10
11
12
const winston = require("winston");


const logger = winston.createLogger({
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({ filename: "crash.log" }),
  ],
});


process.on("uncaughtException", (error) => {
fork icon0
star icon1
watch icon1

+ 11 other calls in file

11
12
13
14
15
16
17
18
19
20
21
22
const app = express();


const logger = winston.createLogger();


if (process.env.NODE_ENV === "production") {
    logger.add(new winston.transports.Console({ level: process.env.LOGGING_LEVEL || "silly" }));
} else {
    logger.add(
        new winston.transports.Console({
            format: winston.format.combine(
fork icon0
star icon1
watch icon0

107
108
109
110
111
112
113
114
115
116
  })
);

if (params && params.token) {
  transports.push(
    new winston.transports.Logentries({
      level: 'info',
      token: params.token,
      region: 'eu',
      json: true,
fork icon2
star icon4
watch icon4

88
89
90
91
92
93
94
95
96
97
		);
	}
}
if (this.log_to_console) {
	if (this.log_format === logFormatEnum.formats.JSON) {
		this.logger.add(new winston.transports.Console({ format: winston.format.json() }));
	} else {
		this.logger.add(
			new winston.transports.Console({
				format: winston.format.combine(
fork icon3
star icon1
watch icon5

+ 15 other calls in file

11
12
13
14
15
16
17
18
19
20
21
22
23


const getLogToProcess = (fileOpt, consoleOpt) => {
	const logArray = [];


	logArray.push(
		new winston.transports.File(fileOpt),
		new winston.transports.Console(consoleOpt),
	);


	return logArray;
fork icon0
star icon3
watch icon1

+ 21 other calls in file

245
246
247
248
249
250
251
252
253
254
    splat(),
    timestamp(),
    logFormat
),
transports: [
    new winston.transports.Console({format: combine( timestamp(), logFormat ), }),
],
exceptionHandlers: [    // handle Uncaught exceptions
    new winston.transports.Console({format: combine( timestamp(), logFormat ), }),
],
fork icon1
star icon5
watch icon6

+ 2 other calls in file

19
20
21
22
23
24
25
26
27
28
app.use(express.json())
app.use(cors())
app.use(expressWinston.logger({
    transports: [
        new winston.transports.Console(),
        new winston.transports.File({ filename: "logs/latest.log" })
    ],
    format: winston.format.combine(
        winston.format.colorize(),
        winston.format.json()
fork icon0
star icon1
watch icon1

+ 3 other calls in file

16
17
18
19
20
21
22
23
24
25
26
27
28




const winston = require('winston');
const packageInfo = require('../../package.json');


winston.add(new winston.transports.Console({
	format: winston.format.combine(
		winston.format.splat(),
		winston.format.simple()
	),
fork icon0
star icon0
watch icon1

4
5
6
7
8
9
10
11
12
13
14
15
const loggingLevel = (process.env.LOGGING_LEVEL || 'info').toLowerCase();


/** @type {import("winston").LoggerOptions} */
const loggerOptions = {
	level: loggingLevel,
	transports: [new winston.transports.Console()]
};


// NOTE: No need to set format to JSON when otel enabled as Node SDK instruments winston automatically
//       to use JSON format, with logging correlation.
fork icon0
star icon0
watch icon0

+ 4 other calls in file