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

@sumor/logger

v1.2.10

Published

This is a lightweight logger for Node.JS. It can output logs in different levels, and you can customize the scope, id, and timezone.

Downloads

275,320

Readme

logger

A Sumor Cloud Tool.
More Documentation

This is a lightweight logger for Node.JS. It can output logs in different levels, and you can customize the scope, id, and timezone.

CI Test Coverage Audit

Installation

npm i @sumor/logger --save

Prerequisites

Node.JS version

Require Node.JS version 18.x or above

require Node.JS ES module

As this package is written in ES module, please change the following code in your package.json file:

{
  "type": "module"
}

Usage

General Usage

import Logger from '@sumor/logger'
const logger = new Logger()

logger.trace('Hello World!')
// You will see the following output:
// 2020-01-01 00:00:00.000 TRACE MAIN - Hello World!

Change Scope

For some case, we need categorize logs. scope is used for this purpose.

import Logger from '@sumor/logger'
const logger = new Logger({
  scope: 'DEMO'
})
logger.trace('Hello World!')
// You will see the following output:
// 2020-01-01 00:00:00.000 TRACE DEMO - Hello World!

Identifier User

For some case, we need identifier user. id is used for this purpose.

import Logger from '@sumor/logger'
const logger = new Logger({
  id: 'USER001'
})
logger.trace('Hello World!')
// You will see the following output:
// 2020-01-01 00:00:00.000 TRACE MAIN USER001 - Hello World!

Change Level

Most of the time, we only need to output logs of a certain level. Then we can decide if store and display it or not.

import Logger from '@sumor/logger'
const logger = new Logger()
logger.trace('Hello World!') // trace is the lowest level, all logs will be output
logger.debug('Hello World!')
logger.info('Hello World!')
logger.warn('Hello World!')
logger.error('Hello World!')
logger.fatal('Hello World!') // fatal is the highest level, only critical error will be output

Change Timezone

import Logger from '@sumor/logger'
const logger1 = new Logger({
  offset: 2 * 60 // UTC+2 offset is 2 hours
})
logger1.info('Hello World!')
// You will see the following output:
// 2020-01-01 02:00:00.000 INFO MAIN - Hello World!

const logger2 = new Logger({
  offset: 8 * 60 // UTC+8 offset is 8 hours
})
logger2.info('Hello World!')
// You will see the following output:
// 2020-01-01 08:00:00.000 INFO MAIN - Hello World!

Predefined Code

import Logger from '@sumor/logger'
const code = {
  trace: {
    HTTP_ACCESS: 'The user accesses via HTTP and the IP address is {ip}'
  },
  debug: {
    USER_TOKEN_LOADED: 'The user login information is read and the user ID is {id}'
  },
  info: {
    USER_LOGIN: 'The user logs in and the user ID is {id}'
  },
  warn: {
    USER_LOGOUT: 'The user logs out and the user ID is {id}'
  },
  error: {
    USER_LOGIN_FAILED: 'The user login failed and the user ID is {id}'
  },
  fatal: {
    USER_LOGIN_BLOCKED: 'The user login is blocked and the user ID is {id}'
  }
}
const i18n = {
  zh: {
    USER_LOGIN: '用户登录,用户ID为{id}'
  }
}
const logger1 = new Logger({
  code,
  i18n
})

logger1.code('USER_LOGIN', { id: 'USER001' })
// You will see the following output:
// 2020-01-01 00:00:00.000 INFO MAIN - The user logs in and the user ID is USER001

const logger2 = new Logger({
  code,
  i18n,
  language: 'zh-US'
})

logger2.code('USER_LOGIN', { id: 'USER001' })
// You will see the following output:
// 2020-01-01 00:00:00.000 INFO MAIN - The user logs in and the user ID is USER001

const logger3 = new Logger({
  code,
  i18n,
  language: 'zh-CN'
})

logger3.code('USER_LOGIN', { id: 'USER001' })
// You will see the following output:
// 2020-01-01 00:00:00.000 INFO MAIN - 用户登录,用户ID为USER001

Global Language Setting

process.env.LANGUAGE = 'zh-CN'
import Logger from '@sumor/logger'

const code = {
  info: {
    USER_LOGIN: 'The user logs in and the user ID is {id}'
  }
}
const i18n = {
  zh: {
    USER_LOGIN: '用户登录,用户ID为{id}'
  }
}
const logger = new Logger({
  code,
  i18n
})

logger.code('USER_LOGIN', { id: 'USER001' })
// You will see the following output:
// 2020-01-01 00:00:00.000 INFO MAIN - 用户登录,用户ID为USER001

Filter Level

When you want to filter logs by level, you can use the following code:

import Logger from '@sumor/logger'
const logger = new Logger({
  level: 'info'
})
logger.trace('Hello World!') // trace is the lowest level, will not be output
logger.debug('Hello World!') // debug is lower than info, will not be output
logger.info('Hello World!') // info is the same as info, will be output
logger.warn('Hello World!') // warn is higher than info, will be output
logger.error('Hello World!') // error is higher than info, will be output
logger.fatal('Hello World!') // fatal is the highest level, will be output

When you use this library cross multiple libraries, you can use the following code:

import Logger from '@sumor/logger'
const logger = new Logger()
process.env.LOG_LEVEL = 'info'

logger.trace('Hello World!') // trace is the lowest level, will not be output
logger.debug('Hello World!') // debug is lower than info, will not be output
logger.info('Hello World!') // info is the same as info, will be output
logger.warn('Hello World!') // warn is higher than info, will be output
logger.error('Hello World!') // error is higher than info, will be output

process.env.LOG_LEVEL = 'warn' // real-time change log level
logger.info('Hello World!') // info is lower than warn, will not be output