@kurai-io/logger
v0.0.5
Published
Logger with colored output for both node and web applications
Downloads
200
Readme
Logger
Logger with colored output for both node and web applications
Table of contents
- Use logger with browser environment
- Use logger with node environment
- Configuring logger
- Exporting stored logs
- Available logging methods
- License
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")
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
Configuring the logger
If you want to remove or change the prefix, you can change the configuration of the logger
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
})
This method will reconfigure all defined loggers. 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
})
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 messagetimestamp
- javascript unix timestamp (milliseconds)level
- log level equal toLogLevel
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
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.