npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nest-simple-logger

v0.0.3

Published

Simple NestJS logging module

Downloads

2

Readme

Nest Simple Logger

Simple logger module for NestJS with support for console and file logging. Includes LoggerInterceptor for logging requests.

Logger supports string interpolation. Error messages are printed to stderr, all other levels to stdout.

Installation

$ npm install nest-simple-logger

How to use

LogModule.forRoot(options)

@Module({
    imports: [
        LogModule.forRoot({
            level: 'debug',
            levels: { AppModuleContext: 'debug' },
            file: path.join(process.cwd(), 'app.log'),
            requestLogger: true,
        }),
    ],
})
export class AppModule {
    private readonly log = new Logger('AppModuleContext');
}

LogModule.forRootAsync(options)

@Module({
    imports: [
        AppConfigModule,
        LogModule.forRootAsync({
            inject: [AppConfigService],
            useFactory: (appConfig: AppConfigService) => {
                const { level, levels, path, file } = appConfig.log;
                let filePath;
                if (file) {
                    filePath = path + '/' + (typeof file === 'string' ? file : 'app.log');
                }
                return {
                    level,
                    levels,
                    file: filePath,
                    requestLogger: true,
                };
            },
        }),
    ],
})
export class AppModule {}

Custom log function

@Module({
    imports: [
        LogModule.forRoot({
            level: 'debug',
            logFn(date: Date, level: LogLevel, context: string, message: string, stackTrace: string | undefined) {
                const req = https.request(
                    {
                        hostname: 'mylogapi.org',
                        path: '/log',
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                    },
                    (res) => {
                        if (res.statusCode < 400) {
                            console.log('Log written to mylogapi.org!');
                        }
                    }
                );
                req.on('error', () => {
                    console.error('Failed!');
                });
                req.write(JSON.stringify({ date, level, context, message, stackTrace }));
                req.end();
            },
        }),
    ],
})
export class AppModule {}

Log message format

Console output:

Formatter

File output:

2023-01-16T17:11:35.150Z VERB  [15356] AnalyticsService : Log message.
2023-01-16T17:11:35.150Z DEBUG [15356] AnalyticsService : Log message.
2023-01-16T17:11:35.150Z LOG   [15356] AnalyticsService : Log message.
2023-01-16T17:11:35.152Z WARN  [15356] AnalyticsService : Log message.
2023-01-16T17:11:35.153Z ERROR [15356] AnalyticsService : Log message.

Message interpolation

Logger supports string interpolation to simplify building of string messages. This can be used to increase performance for low level log messages in production because interpolation is executed only if message is printed to the console or file.

log.error('string: {0}, number: {1}, first text param again: {0}.', 'str_param', 25);
// 2023-01-16T17:44:01.185Z ERROR [12100] AnalyticsService : string: str_param, number: 25, first text param again: str_param.

log.error('boolean: {0}, date: {1}, bigint: {2}.', true, new Date(), BigInt(2));
// 2023-01-16T17:44:01.186Z ERROR [12100] AnalyticsService : boolean: true, date: 2023-01-16T17:44:01.172Z, bigint: 2.

log.error('array: {0}, object: {1}, symbol: {2}.', [1, 2, 3], { foo: 'bar', baz: 3 }, Symbol('sym'));
// 2023-01-16T17:44:01.186Z ERROR [12100] AnalyticsService : array: [1,2,3], object: {"foo":"bar","baz":3}, symbol: sym.

log.error('null: {0}, undefined: {1}, missing param: {2}.', null, undefined);
// 2023-01-16T17:46:17.120Z ERROR [16508] AnalyticsService : null: null, undefined: undefined, missing param: {2}.

Options

| Name | Description | Default | | ------------- | ------------------------------------------------------------------------- | --------- | | level | Default minimum logged level. | undefined | | levels | Map of context: LogLevel. Overrides default level for a specific context. | undefined | | file | Absolute path to a log file. If not specified, log file won't be used. | undefined | | requestLogger | dIf true, each request will be logged with context "REQUEST". | false |