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

@secjs/logger

v1.2.8

Published

> Logger to any NodeJS project

Downloads

60

Readme

Logger 🔍

Logger to any NodeJS project

GitHub followers GitHub stars

The intention behind this repository is to always maintain a Logger package to any NodeJS project.

Installation

npm install @secjs/logger

Usage

Config logging template

First you need to create the configuration file logging in the config folder on project root path. Is extremely important to use export default in these configurations.

import { Env } from '@secjs/env'
import { Path } from '@secjs/utils'

export default {
  /*
  |--------------------------------------------------------------------------
  | Default Log Channel
  |--------------------------------------------------------------------------
  |
  | This option defines the default log channel that gets used when writing
  | messages to the logs. The name specified in this option should match
  | one of the channels defined in the "channels" configuration object.
  |
  */

  default: Env('LOGGING_CHANNEL', 'application'),

  /*
  |--------------------------------------------------------------------------
  | Log Channels
  |--------------------------------------------------------------------------
  |
  | Here you may configure the log channels for your application.
  |
  | Available Drivers: "console", "debug", "file".
  | Available Formatters: "context", "debug", "json", "log".
  |
  */

  channels: {
    application: {
      driver: 'console',
      context: 'Logger',
      formatter: 'context',
    },
    debug: {
      driver: 'debug',
      context: 'Debugger',
      formatter: 'context',
      namespace: 'api:main',
    },
    file: {
      driver: 'file',
      context: 'Logger',
      formatter: 'log',
      filePath: Path.noBuild().logs('secjs.log'),
    },
  },
}

Log / Logger

With the config/logging file created you can use Log and Logger classes to start logging.

import { Log, Logger, Color } from '@secjs/logger'

// Log and Logger will always use the default values of channel inside config/logging, the default channel in here is "application".
Log.log('Hello World!')
// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Logger] Hello World! +0ms

const logger = new Logger()

logger.success('Hello World!')
// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Logger] Hello World! +0ms

// You can pass options to formatters and drivers as second parameter
logger.warn('Hello World!', { color: Color.purple, context: 'LogController' })
// [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [LogController] Hello World! +0ms

Using other channels

You can use any channel that you configure inside config/logging, SecJS has default channels inside the template file.

Log.channel('debug').log('Hello debug world!', { namespace: 'api:example' })
// api:example [SecJS] - PID: 38114 - dd/mm/yyyy, hh:mm:ss PM [Debugger] Hello debug world! +0ms

Extending drivers, channels and formatters

Nowadays, @secjs/logger has only FileDriver, DebugDriver and ConsoleDriver support, but you can extend the drivers for Logger class if you implement DriverContract interface.

import { DriverContract, FormatterContract, format } from '@secjs/logger'

interface CustomDriverOpts {}

class CustomDriver implements DriverContract {
  private readonly _level: string
  private readonly _context: string
  private readonly _formatter: string
  
  constructor(channel: string) {
    const config = Config.get(`logging.channels.${channel}`)
    
    this._level = config.level || 'INFO'
    this._context = config.context || 'CustomDriver'
    this._formatter = config.formatter || 'context'
  }

  transport(message: string, options?: CustomDriverOpts): void {
    options = Object.assign(
      {},
      {
        level: this._level,
        context: this._context,
        streamType: this._streamType,
      },
      options,
    )

    message = format(this._formatter, message, options)

    process[this._streamType].write(`${message}\n`)
  }
}

Same to extend formatters

class CustomFormatter implements FormatterContract {
  // all the methods implemented from FormatterContract...
}

Constructor is extremely important in your CustomDriver class, it's the constructor that will use the values from config/logging channels to manipulate your CustomDriver using channel and channels method from logger. So if you are building a CustomDriver, and you want to use it, you can create a new channel inside config/logging channels or change the driver from an existing channel.

// extending channels
// config/logging file

export default {
  // default etc...
  
  channels: {
    mychannel: {
      driver: 'custom',
      level: 'INFO',
      formatter: 'context',
      context: 'Logger',
    }
    // ... other disks
  }
}

Now you can build your new driver using Logger class

const driverName = 'custom'
const formatterName = 'custom'
const driver = CustomDriver
const formatter = CustomFormatter

Logger.buildDriver(driverName, driver)
Logger.buildFormatter(formatterName, CustomFormatter)

console.log(Logger.drivers) // ['console', 'debug', 'file', 'custom']
console.log(Logger.formatters) // ['context', 'debug', 'json', 'log', 'custom']

Now, if you have implemented your channel in config/logging, you can use him inside logger

// options of your driver and formatter
const options = {}

// Will use CustomDriver to handle the log actions
logger.channel('mychannel').success('Hello World!!', options)

Made with 🖤 by jlenon7 :wave: