dbfannin / ngx-logger

Angular logger
MIT License
428 stars 110 forks source link

NGXLogger logs point to console.log in ngx-logger.mjs library file #333

Open tomatemeo opened 11 months ago

tomatemeo commented 11 months ago

I would expect the logs to point to the position in the code, where the logger is called and not to the code line in the library where the console.log() is called in node_modules/ngx-logger/fesm/ngx-logger.mjs. However, in Chrome dev tools it does just that. Is there a setting I am missing, or anything else I could be doing wrong?

Screenshot 2023-11-28 at 18 34 45

Any help appreciated

bmtheo commented 11 months ago

The logger gives you the position of your code in the []

In your case it is 8450.js:1:3363

tomatemeo commented 11 months ago

Thank for your reply @bmtheo ! I understand that, but the problem is that this is an Angular project, and 8450.js isn't that meaningful. The sourcemap apparently translates that to ngx-logger.mjs. However, if I want to use it instead of e.g. console.debug() that information is not helpful. I would like a behaviour as if I would have written console.debug() when using logger.debug(). Is this possible?

bmtheo commented 11 months ago

Did you :

tomatemeo commented 11 months ago

Thank you so much @bmtheo , the enableSourceMaps: truesetting worked!

tomatemeo commented 11 months ago

The link now shows nicely in the square brackets, but I noticed, that it is not clickable because (at least in Google Chrome) the '.' at the start of the relative path is not recognised as being part of it. Sorry to ask again, but any ideas how to change this?

Screenshot_ngxlogger

Thanks again for your help!

toerni commented 4 months ago

I have the same problem: image The path that is displayed is correct, but when I copy the link by right clicking and copying the link, the value for the screenshot above is /app/infra/logging/logger-config.service.ts. The src part is dropped

tomatemeo commented 4 months ago

Just noticed, I never documented how I solved it in my case. I looked at where the links normally point to for console.logs and wrote a custom writer service to replace the '.' with the corresponding webpack prefix, that works well enough for me.

@Injectable()
export class CustomNGXLoggerWriterService extends NGXLoggerWriterService {

  static readonly PATH_START = /^\.\//
  static readonly WEBPACK_PREFIX = 'webpack:///'

  protected override getFileDetailsToWrite(metadata: INGXLoggerMetadata, config: INGXLoggerConfig): string {
    if (config.disableFileDetails) {
      return '';
    }
    const { fileName, lineNumber, columnNumber } = metadata;
    const webPackFileName = fileName.replace(CustomNGXLoggerWriterService.PATH_START, CustomNGXLoggerWriterService.WEBPACK_PREFIX);
    return `[${webPackFileName}:${lineNumber}:${columnNumber}]`;
  }
}

then in main.ts you can use that writer by:

...createApplication({
  ...
  providers: [
    ...
    LoggerModule.forRoot({ ... }, { 
      writerProvider: { 
        provide: TOKEN_LOGGER_WRITER_SERVICE, 
        useClass: CustomNGXLoggerWriterService     
      }
    }...

If I remember correctly the display problem with the link is not really the fault of ngx-logger, but what the browser dev environment/console interprets as link / part of a link.

tscislo commented 4 months ago

@tomatemeo Thanks for sharing your writer, unfortunately even when I use it I still have the same problem as described by @toerni image

tscislo commented 4 months ago

@tomatemeo

I had to modify your Writer Service to use different PATH_START and WEBPACK_PREFIX

    static readonly PATH_START = /^src\// 
    static readonly WEBPACK_PREFIX = 'http://localhost:4200/src/'

Not very robust though ;)

tscislo commented 3 months ago

Main reason for this issue is that currently Angular CLI in version > 17 uses es-build instead of webpack for bundling