Skip to content
Toan edited this page Feb 17, 2024 · 7 revisions

Creating an ConsoleLogger and ConsoleConfig

To start, you will need to create a new Instance of an ConsoleLogger:

import { ConsoleLogger, ConsoleConfig, FileLogger } from '@fluxbot/better-console'
// or
const { ConsoleLogger, ConsoleConfig, FileLogger } = require("@fluxbot/better-console")

const log = new ConsoleLogger()

But an ConsoleLogger without ConsoleConfig will give you an error, so, you need to create a new ConsoleConfig which looks very similar to an builder:

import { ConsoleLogger, ConsoleConfig, FileLogger ...

const Config = new ConsoleConfig()

const log = new ConsoleLogger(Config)
  • This allows you to create different Loggers ;)

You can also edit 2 parameters with ConsoleConfig:

  • FileLogger
  • Display

FileLogger is the place where better-console will put all the logs (usually an .log file)

For example:

import { ConsoleLogger, ConsoleConfig, FileLogger ...

const Config = new ConsoleConfig()
    .setFileLogger(
        new FileLogger("path/to/log/file")
    )

const log = new ConsoleLogger(Config)

Display is how the package will display in the console The default one is this:

image

But you also can make it you way:

import { ConsoleLogger, ConsoleConfig, FileLogger ...

const Config = new ConsoleConfig()
    .setFileLogger(
        new FileLogger("path/to/log/file")
    )
    .setDisplay("default") // {|} {ts} {type} {:} {msgs}

const log = new ConsoleLogger(Config)

We will come to variables later ;)

Using the logger

To use the logger, you can make a file logger.js or logger.ts where you add the previous code and adding an export in front

Then, you should able to invoke "log" by importing

import { log } from "./logger.js"
// or
const { log } = require("./logger.js")

better-console has 7 built-in levels for you to use:

import { log } from "./logger.js"

log.info("hello world")
log.success("hello world")
log.warn("hello world")
log.startup("hello world")
log.error("hello world")
log.debug("hello world")
log.database("hello world")

Which will give with the default display:

image

Advanced

Creating Custom levels

To create custom levels, you need to use the log.custom function

import { log } from "./logger.js"
import { Colors } from '@fluxbot/better-console'
// or
const { Colors } = require('@fluxbot/better-console')

log.custom("prefix", Colors.Green, "hello world")
//          Prefix       Color        Data

It will give:

image

Colors

better-console has an built-in light-weight color system which allows you to colorize you text !

let Colors = {
    BgBlack: "\u001b[40m",
    BgBlue: "\u001b[44m",
    BgCyan: "\u001b[46m",
    BgGreen: "\u001b[42m",
    BgGrey: "\u001b[90m",
    BgMagenta: "\u001b[45m",
    BgRed: "\u001b[41m",
    BgWhite: "\u001b[47m",
    BgYellow: "\u001b[103m",
    BgOrange: "\x1b[48;5;202m",
    BgClose: "\u001b[49m",

    Black: "\u001b[30m",
    Blue: "\u001b[34m",
    Cyan: "\u001b[36m",
    Green: "\u001b[32m",
    Grey: "\u001b[90m",
    Magenta: "\u001b[35m",
    Red: "\u001b[31m",
    White: "\u001b[37m",
    Yellow: "\u001b[93m",
    Orange: "\x1b[38;5;208m",
    Close: "\u001b[39m",

    Reset: "\u001b[0m",
}

You ABSOLUTLY need to use it like that:

log.info(`${Colors.BgBlue}Hello world in BgBlue !${Colors.BgClose}`) // Backgroud blue (BgClose)
log.info(`${Colors.BgMagenta}Hello world in BgMagenta !${Colors.BgClose}`) // Backgroud magenta (BgClose)

log.info(`${Colors.Blue}Hello world in Blue !${Colors.Close}`) // Text in blue (Close)
log.info(`${Colors.Magenta}Hello world in Magenta !${Colors.Close}`) // Text in magenta (Close)

It will give:

image