nest-x / nestx-log4js

nestjs log4js-node module
MIT License
29 stars 5 forks source link

Nestjs example #519

Open nushrivastava opened 3 years ago

nushrivastava commented 3 years ago

Can you share a nestjs example in your usage along with the currently shared test scenarios. I have already configured log4js, but I needed nest system logs to be pushed to my configured logger for which I came across this library, but I am not able to configure it correctly.
I have added the suggested code of app.use(app.get(Log4jsModule)) in main.ts and in my app.module file I am trying to do

Log4jsModule.forRoot({
      config: {
        appenders: {
          analyticsFileAppender: {
            type: 'file',
            maxLogSize: environment.maxLogSize,
            keepFileExt: true,
            layout: {
              type: 'pattern',
              pattern: '%d %p %z %f{1}:%l %m%n',
            },
            filename: `${environment.logDir}/${environment.logFile}`,
          },
          analyticsConsoleAppender: {
            type: 'console',
            layout: {
              type: 'pattern',
              pattern: '%d %p %z %f{1}:%l %m%n',
            },
          },
        },

        categories: {
          default: {
            appenders: [`${environment.appender}`],
            level: `${environment.logLevel}`,
            enableCallStack: true,
          },
        },
      },
    }),

These configurations I have added to my nest library and simply exported getLogger() value, while trying to reuse the config from there I was not able to use it. Even after hard coding it like above it gives me error

if (category.indexOf('.') > 0) {
               ^
TypeError: category.indexOf is not a function

I have even tried to put the basic config that you have added in your test example usage but stuck with the same issue.

If I only use Log4jsModule.forRoot() I am able to make it work, but it doesnt use my existing logger configurations. (Obviously, but wanted to state that with default it works.) Online support of this library is very less, due to which I am posting this question here, Thanks in advance.

aquariuslt commented 3 years ago

@nushrivastava could you provide a reproduce repo?

I can not local re-produce your error simply using latest @nestjs/cli generated application and modified default app.module.ts and main.ts like below.

app.module.ts

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Log4jsModule } from '@nestx-log4js/core';

@Module({
  imports: [
    Log4jsModule.forRoot({
      config: {
        appenders: {
          analyticsFileAppender: {
            type: 'file',
            maxLogSize: 20000,
            keepFileExt: true,
            layout: {
              type: 'pattern',
              pattern: '%d %p %z %f{1}:%l %m%n',
            },
            filename: `logs/api-server.log`,
          },
          analyticsConsoleAppender: {
            type: 'console',
            layout: {
              type: 'pattern',
              pattern: '%d %p %z %f{1}:%l %m%n',
            },
          },
        },

        categories: {
          default: {
            appenders: ['analyticsFileAppender', 'analyticsConsoleAppender'],
            level: `debug`,
            enableCallStack: true,
          },
        },
      },
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

main.ts

import { NestFactory } from '@nestjs/core';

import { AppModule } from './app/app.module';
import { Log4jsLogger } from '@nestx-log4js/core';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const globalPrefix = 'api';
  const port = process.env.PORT || 3333;

  app.setGlobalPrefix(globalPrefix);
  app.useLogger(app.get(Log4jsLogger));

  const logger = app.get(Log4jsLogger);

  await app.listen(port, () => {
    logger.log('Listening at http://localhost:' + port + '/' + globalPrefix);
  });
}

bootstrap();

console output

[Nest] 28091   - 06/27/2021, 1:55:36 AM   [NestFactory] Starting Nest application...
[Nest] 28091   - 06/27/2021, 1:55:36 AM   [InstanceLoader] AppModule dependencies initialized +115ms
[Nest] 28091   - 06/27/2021, 1:55:36 AM   [InstanceLoader] Log4jsModule dependencies initialized +0ms
2021-06-27T01:55:36.629 INFO 28091 routes-resolver.js:38 AppController {/api}:

2021-06-27T01:55:36.637 INFO 28091 router-explorer.js:84 Mapped {/api, GET} route

2021-06-27T01:55:36.640 INFO 28091 nest-application.js:97 Nest application successfully started

2021-06-27T01:55:36.660 INFO 28091 events.js:375 Listening at http://localhost:3333/api
Lukunlin commented 2 years ago

i use for categories don't take effect