fullstack-build / tslog

📝 tslog - Universal Logger for TypeScript and JavaScript
https://tslog.js.org
MIT License
1.35k stars 63 forks source link

Feature Request: Print objects to console without expanding them. #263

Open avin-kavish opened 1 year ago

avin-kavish commented 1 year ago

So when we normally log to console using console.log, browsers print the objects in a contracted format, and you can optionally click to expand to inspect the properties of the object.

Can I get the same behavior with tslog? I scanned through the documentation but couldn't figure out whether that was 100% possible.

shakursmith commented 1 year ago

@avin-kavish you can do this pretty easily by overwriting the default behavior of the logger.

const logger = new Logger({
    overwrite: {
      transportFormatted: (logMetaMarkup: string, logArgs: unknown[], logErrors: string[]): void => {
        console.log(...logArgs);
      }
    } 
});

And if you don't want this to be the default behavior you can create a Sub-Logger

const logger = new Logger({ minLevel: 2 });

export const devSubLogger = logger.getSubLogger({ name: "DevSubLogger", minLevel: 2, overwrite: {
    transportFormatted: (logMetaMarkup: string, logArgs: unknown[], logErrors: string[]): void => {
        console.log(...logArgs);
      }
} });

Then use it like this

import logger, { devSubLogger } from './logger.ts';
...
logger.debug(Accounts) // for expanded logs
devSubLogger.debug(Accounts); // for collapsed logs

It's a pretty simple fix, but I still think this should be a standard feature so others won't have to go to the trouble. It'd be nice to have a type: "plain" option to go with the other options (pretty, json, hidden). Happy to work on it if other's think it will be worthwhile.

terehov commented 1 year ago

I am always happy to merge MRs :-)