sharplogger
v1.0.6
Published
SharpLogger is a simple, easy-to-use, lightweight logging library.
Downloads
12
Maintainers
Readme
SharpLogger
SharpLogger is a simple, easy-to-use, lightweight logging library.
This package provides color support (ANSI colors), pre-set logging functions (error
, warn
, info
, and log
), and an optional file logging functionality that automatically appends to log files whenever log functions are called.
Features
- ANSI colors: Easily apply ANSI colors to your logs (automatically applied to prefixes)
- Pre-set logging functions: Use
error
,warn
,info
, andlog
to standardize your logging output - Optional log file: Automatically append to a log file when a logging function is called, creates during initalization
- Customizable prefix colors: Change the color of the log prefixes using the
setPrefixColor
method
Installation
npm install simplelogger
Example usage
const LogFile = require('simplelogger')
// Create a new file named /log and append a date string.
// Would come out like so: ./log-24-08-09_14-30-45.log (YY-MM-DD_HH-MM-SS)
const logFile = new LogFile('./log', 'log', true);
logFile.initialize(); // only called when filePath is not null
// Set prefix color of informational logs to bright green
logFile.setPrefixColor('info', logFile.colors.fg.brightGreen);
// Test out log functionalities
logFile.log('hello'); // "hello"
logFile.error('world'); // "[ERROR] world" with [ERROR] is bright red
logFile.warn('!!!'); // "[WARN] !!!" with [WARN] is bright yellow
logFile.info(':D'); // "[INFO] :D" with [INFO] is bright blue
Log file output w/ example
Log data @ August 9, 2024 at 02:28:12 AM
-----
[LOG : August 9, 2024 at 02:28:12 AM] hello
[ERROR : August 9, 2024 at 02:28:12 AM] world
[WARN : August 9, 2024 at 02:28:12 AM] !!!
[INFO : August 9, 2024 at 02:28:12 AM] :D
Documentation
LogFile class
Constructor: new LogFile(filePath = null, extension = null, newLogFile = null);
- All of these are optional, if you do not want to use the log file functionality, leave the LogFile constructor empty.
filePath
(String): The directory path with the base file name to create the log file in, eg."./log"
extension
(String): The extension of the file, without the preceding dot, eg."log"
newLogFile
(Boolean): Whether to create a new log file with an appended date string
Methods
initialize()
: Create the log file iffilePath
andnewLogFile
are not null, appending an initial entry with the current date & timesetPrefixColor(functionName, color)
: Set the color for the specific log prefix,functionName
should match any below functions andcolor
can technically be anything, but you should useLogFile.colors
for ANSI colors. You can also use multiple colors, eg.setPrefixColor('error', logFile.colors.fg.brightRed + logFile.colors.fg.bold)
(formatting is not reset)- All log functions will append the message to the log file with a prefix such as [ERROR] or [LOG]
- endProcess can end the process on 3 codes:
- 0: non-error
- 1: error
- 2: warning
log(string, endProcess = false)
: Log a standard message to the consoleerror(string, endProcess = false)
: Log an error to the console with a red-prefixed [ERROR]warn(string, endProcess = false)
: Log a warning message with a yellow-prefixed [WARN]info(string, endProcess = false)
: Log an informational message with a blue-prefixed [INFO]
ANSI Colors
The LogFile class provides all ANSI Colors with an object in the constructor, LogFile.colors.fg
or LogFile.colors.bg
with additional formatting options.
Colors are applied automatically to the following functions:
error
: Bright redwarn
: Bright yellowinfo
: Bright bluelog
: No color