Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

winston

Package Overview
Dependencies
Maintainers
8
Versions
87
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

winston

A logger for just about everything.

  • 3.15.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10M
decreased by-16.73%
Maintainers
8
Weekly downloads
 
Created

What is winston?

Winston is a versatile logging library for Node.js. It is designed to be a simple and universal logging library with support for multiple transports. A transport is essentially a storage device for your logs. Winston allows you to query your logs and can be extended with custom transports, which means that you can write your own storage devices. It is also capable of logging with different levels of severity, which can be useful for filtering logs based on importance.

What are winston's main functionalities?

Logging with different severity levels

This code sets up Winston to log messages of different severity levels, which include error, warn, info, verbose, debug, and silly. It also demonstrates how to add multiple transports; in this case, it logs to two files and optionally to the console when not in production.

{"const winston = require('winston');\nconst logger = winston.createLogger({\n  level: 'info',\n  format: winston.format.json(),\n  transports: [\n    new winston.transports.File({ filename: 'error.log', level: 'error' }),\n    new winston.transports.File({ filename: 'combined.log' })\n  ]\n});\n\nif (process.env.NODE_ENV !== 'production') {\n  logger.add(new winston.transports.Console({\n    format: winston.format.simple()\n  }));\n}\n\nlogger.error('Error log example');\nlogger.warn('Warning log example');\nlogger.info('Information log example');\nlogger.verbose('Verbose log example');\nlogger.debug('Debug log example');\nlogger.silly('Silly log example');"}

Custom log formats

This code demonstrates how to create a custom log format using Winston's format.combine method. It includes a timestamp and prints the log level and message in a custom format.

{"const winston = require('winston');\nconst logger = winston.createLogger({\n  transports: [\n    new winston.transports.Console()\n  ],\n  format: winston.format.combine(\n    winston.format.timestamp({\n      format: 'YYYY-MM-DD HH:mm:ss'\n    }),\n    winston.format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)\n  )\n});\n\nlogger.info('Custom format log example');"}

Querying logs

This code snippet shows how to query logs from a file transport. It retrieves logs from the last 24 hours, limits the results to 10, and orders them in descending order.

{"const winston = require('winston');\nconst logger = winston.createLogger({\n  transports: [\n    new winston.transports.File({ filename: 'combined.log' })\n  ]\n});\n\nlogger.query({\n  from: new Date() - (24 * 60 * 60 * 1000),\n  until: new Date(),\n  limit: 10,\n  start: 0,\n  order: 'desc',\n}, function (err, results) {\n  if (err) {\n    throw err;\n  }\n  console.log(results);\n});"}

Other packages similar to winston

Keywords

FAQs

Package last updated on 05 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc