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

@kurai-io/logger

v0.2.1

Published

Logger with colored output for both node and web applications

Downloads

285

Readme

Logger

Logger with colored output for both node and web applications

Table of contents

Environment-based setup

This section describes how to customize the logger to fit your environment.

Use logger with browser environment

If you want to use the logger in a browser environment, just call the static methods of the main class (Logger) because the browser logger is always set by default

// my-store.ts
import Logger from "@kurai-io/logger"

Logger.infoFrom("MyModule", "Message from module")

You can also call BrowserLogger directly by accessing the main class getter

import Logger from "@kurai-io/logger"

Logger.browser.info("My message")

Use logger with node environment

If you want to use the logger in a node environment, you must first set up the node logger. The node logger is not defined by default, as it may not work in a browser environment

import Logger from "@kurai-io/logger"

Logger.setupNodeLogger() // This is asynchronous function

You can also pass a configuration object directly to the setup function

import Logger from "@kurai-io/logger"

Logger.setupNodeLogger({
  // Configuration
})

// Once configured, you should be able to call the node logger

Logger.node?.info("My message")

// or

Logger.unsafeNode.info("My message")

Configuring the logger

This section describes the process of customizing the logger to your needs

Changing default logger

The default logger (BrowserLogger) will be used by the logging methods of the main class (Logger). You can change the default logger to determine which logger will be used by the default logging methods

Logger.setDefaultLogger(Logger.browser)
// or
Logger.setDefaultLogger(Logger.unsafeNode)

Note: you must configure the node logger before using the logging methods, otherwise you will get an error at runtime

Reconfiguring logger

This method will reconfigure all defined loggers.

import Logger from "@kurai-io/logger"

Logger.reconfigure({
  /*
   * If set as false, log prefix will be completely hidden
   *
   * Default is true
   * */
  showPrefix: true,

  /* If set to true, log time will be attached into prefix */
  attachPrefixTime: true,

  /* If set to true, log date will be attached into prefix */
  attachPrefixDate: true,

  /* Define custom logs storage */
  storage: LogsStorage
})

If you only want to reconfigure a specific logger, call the same method on the of the desired logger instance

import Logger from "@kurai-io/logger"

Logger.browser.reconfigure({
  // Configuration
})

Named logger instance

If you plan to output many messages from a single component of your application, you may want to get a named logger instance to avoid constantly specifying namespace

const logger = Logger.named("MyNamespace")

// Only info, warning, error and success methods are availble
logger.info("Hello world!") // => [Info from MyNamespace] Hello world!

Exporting stored logs

You can access the logs you received throughout the session

import Logger from "@kurai-io/logger"

Logger.exportStoredLogs()

The method will return an object with keys equal to the keys of the LogLevel enumeration, each containing an array of StoredLog objects.

The StoredLog object will always contain the following keys:

  • message - converted to string message
  • timestamp - javascript unix timestamp (milliseconds)
  • level - log level equal to LogLevel values

Available logging methods

| Method | Browser prefix color | Node prefix color | |------------------------------------------------|----------------------|-------------------| | info(...message: any[]) | cornflowerblue | cyan | | infoFrom(from: string, message: any[]) | cornflowerblue | cyan | | warning(...message: any[]) | orange | yellow | | warningFrom(from: string, ...message: any[]) | orange | yellow | | error(...message: any[]) | red | red | | errorFrom(from: string, ...message: any[]) | red | red |

Note: colors will not be used if the prefix is completely disabled

Note 2: only methods without the From postfix are available for the named logger

License

You can copy and paste the MIT license summary from below.

MIT License

Copyright (c) 2022-2024 Kurai Foundation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.