Skip to main content

winston

@ensono-stacks/logger:winston

Add Winston to your project
Generates a new Nx library which contains a Winston logger instance and associated config.

Prerequisites

An existing Stacks workspace.

Usage

nx g @ensono-stacks/logger:winston

Command line arguments

The following command line arguments are available:

OptionDescriptionTypeAccepted ValuesDefaultAvailable in interactive prompt?
--nameName of the generated librarystringN/AYes
--tagsAdd tags to the project (used for linting)stringN/A
--directoryDirectory where the project is placed (within Nx libs directory)stringN/A
--skipFormatSkip formatting filesbooleantrue/falsefalse
--logLevelTypeThe type of log levels that will be usedenumcli/syslog/npmnpmYes
--consoleLogsOutput logs to the consolebooleantrue/falsefalse
--fileTransportPathFile path used for logs transportstringN/A
--httpTransportAdd a http transportbooleantrue/falsefalse
--httpTransportHostRemote host of the HTTP logging endpointstringN/A
--httpTransportPortRemote port of the HTTP logging endpointnumberN/A
--httpTransportPathRemote URI of the HTTP logging endpointstringN/A
--httpTransportSSLUse SSL for the HTTP logging endpointbooleantrue/falsefalse
--streamPathStream transport pathstringN/A

Generator Output

The generator will create a new application within your libs folder with the following structure:

.
├── libs/[libname]
│ ├── src
│ ├── ├── index.ts // Contains the Winston configuration and creates the logger instance
│ ├── ├── index.test.ts // Tests for the logger
│ ├── .eslintrc.json // ESLint config - extends from workspace config
│ ├── jest.config.ts // Jest config - extends from workspace config
│ ├── project.json // Nx config file for the library
│ ├── tsconfig.json // Main Typescript config for the library - extends workspace config & references the below two tsconfig files
│ ├── tsconfig.lib.json // Typescript config for the library's source files (excluding tests)
│ ├── tsconfig.spec.json // Typescript config for the library's test files
│ ├── README.md // Information on the library and how to run scripts
├── jest.config.ts // Workspace-level Jest config - created if this does not already exist
└── jest.preset.ts // Workspace-leve Jest preset that extends `@nrwl/jest/preset` - created if this does not already exist.

Additionally, the following files will be modified

.
├── nx.json // Adds configuration for Jest tests if this has not already been done by another generator
├── package.json // Adds winston as a dependency
└── tsconfig.base.json // Adds new library into `paths` field

Importing the logger into your app

Having created a logger using the above command, import the Winston logger instance from the newly created library (the import name can be found within the tsconfig.base.json files paths field) into your application:

import logger from '@workspace-name/mynewlogger'

logger.log({
level: 'info',
message: 'I love Ensono Stacks!',
})

To change how Winston is configured, edit the created library:

./libs/mynewlogger/src/index.ts
const logger = winston.createLogger(logConfiguration);

// Custom transport for non-production
if (process.env.NODE_ENV !== 'production') {
logger.add(new winston.transports.Console({
format: winston.format.simple(),
}))
}

export default logger;

Other resources

Documentation for Winston can be found here.