0.1.4 â€ĸ Published 9 months ago

@future-widget-lab/ui-logger v0.1.4

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

@future-widget-lab/ui-logger

An opinionated but minimal logger built on top of loglevel. It allows you to define custom log levels, emojis for different log types, and control debug level logs based on URL search parameters.

Features

  • Custom log levels and message formatting.
  • Debug logging controlled by URL search parameters.
  • Fine-grained control over debug visibility using tags.
  • Emoji support for different log types.
  • Optional side effects after a message is printed.

Installation

npm install @future-widget-lab/ui-logger

Usage

Basic Example

import { createLogger } from '@future-widget-lab/ui-logger';

const logger = createLogger({
  level: 'info'
});

logger.info({}, 'Application started');
logger.error({ errorCode: 500 }, 'Internal server error');

Debugging

To enable debug logs for a specific tag, add the corresponding search parameter to the URL.

const logger = createLogger({
  level: 'debug',
  allTag: 'all',
  debugSearchParameterName: 'debug'
});

logger.debug('auth-module', { user: 'test' }, 'Authentication successful');

To see the debug logs, navigate to:

http://example.com/?debug=auth-module

Or use the catch-all tag:

http://example.com/?debug=all

Customizing Log Emojis

const logger = createLogger({
  level: 'info',
  debugEmoji: '🐛',
  errorEmoji: '❌',
  infoEmoji: 'â„šī¸',
  traceEmoji: '🔍',
  warnEmoji: 'âš ī¸'
});

Handling Log Side Effects

You can perform custom actions after a log message is printed by using the onAfterMessage option. The logger handles formatting, while onAfterMessage can trigger side effects such as API calls.

const logger = createLogger({
  level: 'info',
  onAfterMessage: ({ level, timestamp, payload, message }) => {
    sendLogToAnalytics({ level, timestamp, payload, message });
  }
});

API Reference

createLogger(options: CreateLoggerOptions)

Creates a new logger instance with the specified configuration options.

Options

OptionTypeDefaultDescription
levelLogLevelDescSets the default log level.
allTagstring'all'Catch-all tag for debug logs.
debugSearchParameterNamestring'debug'Name of the search parameter for debug logs.
shouldDebugShouldDebugInternal HelperCustom function to determine if debug logs should be printed.
debugEmojistring'đŸ•ĩ'Emoji for debug logs.
errorEmojistring'📕'Emoji for error logs.
infoEmojistring'📘'Emoji for info logs.
traceEmojistring'📓'Emoji for trace logs.
warnEmojistring'📒'Emoji for warn logs.
onMessagefunctionundefinedCustom message formatting handler.
onAfterMessagefunctionundefinedSide-effect handler after logging.

Methods

  • debug(tag: string, object: object, message: string): Logs a debug message.
  • error(object: object, message: string): Logs an error message.
  • info(object: object, message: string): Logs an informational message.
  • trace(object: object, message: string): Logs a trace message.
  • warn(object: object, message?: string): Logs a warning message.

License

MIT