Skip to content

Customization

Text Styling

Colored Output

When creating a new instance of the logger using the createNewLogger() function, setting the colored to false will disable colored output in the terminal. Below is an example for that:

1
2
3
4
// Create a new instance of the Logger
let logger = new itivruttah.createNewLogger({
    colored: false
});

Outputting colored messages can even be changed in the runtime. By setting the following property to true or false appropriately.

1
logger.config.colored = false; // Or true if you want to enable logging colorful messages.

By default colored is set to true.

Bold Style

To Disable bold styling and output only in normal text weight, pass the following key when creating the logger instance.

1
2
3
4
// Create a new instance of the Logger
let logger = new itivruttah.createNewLogger({
    boldText: false
});

Itivruttah supports having bold text for the message type string and normal text for other values. Though for the message, a preformatted string is accepted and further styling can be tweaked by modifying the theme.

Below is an example on how you can dynamically turn off/on outputting bold styled text once the logger instance has already been created.

1
logger.config.boldText = false;

By default boldText is set to true.

Time & Date Formatting

The properties timeFormat and dateFormat control how time and date are outputted, both in the terminal and the log file (if configured). Incase your theme does not output time or date, then these properties will be ignored. Below is an example, to output the full date and time instead of the default one:

1
2
3
4
5
// Create a new instance of the Logger
let logger = new itivruttah.createNewLogger({
    timeFormat: "h:MM:ss TT Z",
    dateFormat: "dddd, mmmm d, yyyy"
});

For further customization of the properties timeFormat and dateFormat, please visit this link.

Note

The properties timeFormat and dateFormat are two independent properties, and both need not be specified at the same time.

By default timeFormat is set to hh:MM:ss and dateFormat is set to dd-mm-yyyy.

File Logging

Though currently Itivruttah only supports single file logging and does not support buffered log writing, in future these feature will be certainly implemented. To start logging the messages to a text file, pass and set the logToFile property to true, when creating a new instance of the logger.

1
2
3
4
// Create a new instance of the Logger
let logger = new itivruttah.createNewLogger({
    logToFile: true
});

Here is an example to prevent a single/few/certain message(s) from being logged into the log file, but still show up in the terminal:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Your code ...

// Set the logToFile property to false
logger.config.logToFile = false;

// This message will only show up in the terminal
// But will not be written in the log file
logger.logInfo("The message!");

// Re-enable log file writing again, so other messages will get logged in the file
logger.config.logToFile = true;

// You code ...

By default logToFile is set to false.

Custom Log File Path

By default Itivruttah will try reading the package.json file of the dependent project to get the name of the project and write the log file in the dependent project's root directory. The filename of the created log file will be [project_name].log

To override the log file path, an optional value with the name logFile is set to the absolute path of the log file, when creating a new instance of the logger.

1
2
3
4
5
// Create a new instance of the Logger
let logger = new itivruttah.createNewLogger({
    logToFile: true,
    logFile: "/home/vasanth/nirvahaka/nirvahaka.log" // Replace it with the path of the log file to be written
});

Header Text

When creating the log file for the first time, an optional header text can be written first which will be followed by the logs.

Tip

The header text is a good place where you can output some ascii-art text or some useful information about the application's version, etcetera.

1
2
3
4
5
// Create a new instance of the Logger
let logger = new itivruttah.createNewLogger({
    logToFile: true,
    logFileHeader: headerStr // A variable where some cool ascii-art or useful info is stored.
});

By default logFileHeader is set to null.

Themes

Itivruttah can be configured to display the log messages in many different layouts. A theme is a template string that will be used to construct the actual log messages. One of the best features of Itivruttah is that the theme can be changed dynamically in the runtime. Below is an example of using a custom theme:

1
2
3
4
// Create a new instance of the Logger
let logger = new itivruttah.createNewLogger({
    theme: "$DATE | $TIME | $TYPE | $MESSAGE"
});

With the default values for dateFormat and timeFormat, the above theme will look similar to this:

1
20-10-2020 | 05-01-26 | INFO | An information message.

Predefined Themes

Below are a few pre-made themes for you to use. You are welcome to contribute your own theme by clicking the following link.

1
2
3
$FILENAME   $TYPE   $MESSAGE
------------------------------------------------------------
app.js   WARNING   Something needs to be taken care of.
1
2
3
* $TYPE $MESSAGE
------------------------------------------------------------
* VERBOSE A message useful to developers.
1
2
3
$DATE $TIME [$TYPE]: $MESSAGE
------------------------------------------------------------
Oct 20 05-01-27 [INFO]: Can be configured to look in anyway!
1
2
3
$TYPE $MESSAGE
------------------------------------------------------------
SUCCESS Messages that make everyone happy :)

Creating Custom Themes

Creating a custom theme is pretty easy with the following variables. Just arrange them in the way you want.

Date ( $DATE ) Outputs the current date by formatting it with the dateFormat option.

Time ( $TIME ) Outputs the current time by formatting it with the timeFormat option.

Filename ( $FILENAME ) Outputs the file name from where the log statement was called.

Message ( $MESSAGE ) Outputs the log message taken from an nodeJS Error Object, a String, or an array/object.

Message Type ( $TYPE ) Outputs the message type, ex. WARNING, INFO, FATAL etcetera. If the property colored is set to true, a colored output will be presented.

NodeJS Binary Path ( $NODE ) Outputs the NodeJS executable path. Useful when compiling the project with something like pkg.

Script File Path ( $SCRIPT_PATH ) Outputs the main script file's name with extension.