Skip to content

Winston Config

The WinstonConfig is a configuration module used to set up and customize the Winston logging system for your application. It provides a flexible way to configure the logging format, transports, and integration with Google Cloud Logging. This configuration makes it easy to manage logs within your application, ensuring that the logs are properly formatted, stored, and integrated with external services when needed.

The WinstonConfig is utilized by the WinstonModule.

How to create and customize WinstonConfig

Here's a list of options for the createWinstonConfig function:

  • appName: (string) The name of your application, which is used as a prefix in the log messages to easily identify the source.

  • googleLogName: (string) The name of the log in Google Cloud Logging, which is used to store logs when your application is not running in a local environment.

Here's an example of how to create a WinstonConfig using the createWinstonConfig function:

ts
// src/config/index.ts
import { createWinstonConfig } from '@deeepvision/nest-kit/dist/configs';

export const WinstonConfig = createWinstonConfig({
  appName: 'Jetstream',
  useConsole: process.env.APP_ENV === AppEnv.LOCAL,
  useGoogleCloudLogging: process.env.APP_ENV !== AppEnv.LOCAL,
  googleCloudLoggingOptions: {
    logName: 'jetstream_core',
  },
});

How to use WinstonConfig for WinstonModule initialization:

  1. In your app.module.ts, import the required modules and configurations:
ts
import { WinstonModule } from '@deeepvision/nest-kit';
import * as configs from './config';
  1. Inside the @Module decorator, use the WinstonModule.forRootAsync() method to initialize the WinstonModule with the WinstonConfig. Pass a configuration object with the following properties:
  • useFactory: An async factory function that takes injected opts (a ConfigType of WinstonConfig) as an argument.
  • inject: An array containing the injection token configs.WinstonConfig.KEY to properly inject the DbConfig.

Here's the complete code:

ts
// src/app.module.ts
import { WinstonModule } from '@deeepvision/nest-kit';
import { ConfigModule, ConfigType } from '@nestjs/config';
import * as configs from '@/config';

@Module({
  imports:[
    // Other modules

    // Configure the WinstonModule with Winston config
    WinstonModule.forRootAsync({
      inject: [configs.WinstonConfig.KEY],
      useFactory: async (opts: ConfigType<typeof configs.WinstonConfig>) => opts,
    }),
  ],
  ...
})
export class AppModule {}

By following these steps, you'll be able to utilize the WinstonConfig for initializing the WinstonModule with the appropriate logger settings.

Created by DeepVision Software.