loggedmessage
v1.2.0
Published
a simple but flexible console logging library with common-sense builtin functions and defaults
Downloads
3
Maintainers
Readme
About loggedmessage
loggedmessage
is a simple but flexible console logging library written in JavaScript with no dependencies. loggedmessage
comes with common-sense builtin functions and defaults, and works right out of the box.
The package provides you with functions logm()
, infom()
and timem()
to log messages to the console with standard formatting, as well as functions errm()
, warnm()
and throwm()
for error handling. Also included is printm()
, a simple wrapper for console.log()
that returns an array of the arguments it's given.
Install
Install loggedmessage
via NPM:
$ npm i loggedmessage
Basic Usage
Import:
Import the library in your code:
import loggedmessage from "loggedmessage";
// or
import { logm, errm, warnm, infom, timem, printm, throwm } from "loggedmessage";
loggedmessage
must be imported as an ES6 module.
Logging:
To log to the console:
import lm from "loggedmessage";
lm.logm("This is a log message");
lm.errm("Failed to connect to server");
lm.warnm("Request for /:user failed without authorization");
lm.infom("Server listening on port 3000");
lm.timem("Received a GET request for /");
lm.printm("A regular console.log() statement");
lm.throwm("Invalid configuration file");
Output:
LOGM: This is a log message
ERRM: Failed to connect to server
WARNM: Request for /:user failed without authorization
INFOM: Server listening on port 3000
12/31/1999, 12:35:00 PM: Received a GET request for /
A regular console.log() statement
THROWM: Invalid configuration file
<stackTrace>
throw new Error(`${toString(errPrefix, errSeparator)} ${checkObject(errMessage)}`);
^
Error: THROWM: Invalid configuration file
at <stackTrace>
Configuration
If dotenv
is installed in the project, message defaults can be configured using a .env
file:
### Prefix abbreviation
### (leave commented to not print a prefix abbreviation)
PREFIX_ABR = "APP"
### Separator between abbreviation and prefix
### (leave commented to use the default separator)
PREFIX_SEPARATOR = "-"
### Default message type prefixes
### (leave commented to use the default prefixes)
LOGM_PREFIX = "LOG"
ERRM_PREFIX = "ERROR"
THROWM_PREFIX = "ERROR-THROWN"
WARNM_PREFIX = "WARNING"
INFOM_PREFIX = "INFORMATION"
### Default separator between the prefix and message
### (leave commented to use the default separator)
MESSAGE_SEPARATOR = " ::"
### Default text for undefined messages
### (leave commented to use the default text)
LOG_MESSAGE = "a new message was logged"
ERROR_MESSAGE = "a new error has occurred"
WARN_MESSAGE = "a new warning was logged"
### Time message locale
### (leave commented to use "en-US")
### ex.: "en-US", "en-GB", "ko-KR", "ar-EG", "ja-JP-u-ca-japanese", etc.
TIMEM_LOCALE = "en-GB"
If dotenv
is not installed, loggedmessage
will use the builtin defaults.
Documentation
logm()
Log a message to the console.
Parameters
message
any
— the message content. (optional, defaultnull
)prefix
string
— the message prefix text. (optional, defaultnull
)separator
string
— the separator string between the prefix and message text. (optional, defaultnull
)
Examples
Code:
logm("logged message", "LOG", " |");
Output:
LOG | logged message
Returns string
— the full message string as {prefix}{separator} {message}
.
errm()
Log an error to the console with the desired message.
Parameters
message
any
— the error message content. (optional, defaultnull
)err
Error
— a target error to print to print. (optional, defaultnull
)prefix
string
— the error message prefix text. (optional, defaultnull
)separator
string
— the separator string between the prefix and error message text. (optional, defaultnull
)
Examples
Code:
errm("error message", new Error("error text"), "ERROR", " |");
Output:
ERROR | error message
Error: error text
at <stackTrace>
Returns string
— the full error message string as {prefix}{separator} {message}
.
warnm()
Log a warning message/error to the console with the desired message.
Parameters
message
any
— the warning message content. (optional, defaultnull
)err
Error
— a target error to print. (optional, defaultnull
)separator
string
— the separator string between the prefix and warning message text. (optional, defaultnull
)
Examples
Code:
warnm("warning message", new Error("error text"), " |");
Output:
WARNM | warning message
Error: error text
at <stackTrace>
Returns string
— the full warning message string as {prefix}{separator} {message}
.
infom()
Log an info message/error to the console with the desired message.
Parameters
message
any
— the info message content. (optional, defaultnull
)err
Error
— a target error to print. (optional, defaultnull
)separator
string
— the separator string between the prefix and info message text. (optional, defaultnull
)
Examples
Code:
infom("information message", undefined, " |");
Output:
INFOM | information message
Returns string
— the full info message string as {prefix}{separator} {message}
.
timem()
Log a message/error to the console with the desired message and the current time.
Parameters
message
any
— the message content. (optional, defaultnull
)err
Error
— a target error to print. (optional, defaultnull
)separator
string
— the separator string between the prefix and message text. (optional, defaultnull
)
Examples
Code:
timem("time-logged message", undefined, " |");
Output:
12/31/1999, 12:35:00 PM | time-logged message
Returns string
— the full message string as {prefix}{separator} {message}
.
printm()
Print a message to the console (wrapper for console.log()
).
Parameters
message
any
— the message content.optionalParams
...any
— additional values or objects for output.
Examples
Code:
printm("printed message", "& additional output");
Output:
printed message & additional output
Returns Array
— an array of the arguments passed.
throwm()
Throw an error with the desired message.
Parameters
message
any
— the error message content. (optional, defaultnull
)err
Error
— a target error to print. (optional, defaultnull
)prefix
string
— the error message prefix text. (optional, defaultnull
)separator
string
— the separator string between the prefix and error message text. (optional, defaultnull
)
Examples
Code:
throwm("thrown error message", new Error("error text"), "ERROR", " |");
Output:
ERROR | thrown error message
<stackTrace>
throwm("thrown error message", new Error("error text"), "ERROR", " |");
^
Error: error text
at <stackTrace>
Returns string
— the full error message string as {prefix}{separator} {message}
.