1.0.0 • Published 5 years ago

tamarine v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

Tamarine

Tamarine is a simple Node.js logging library, inspired on https://github.com/asolera/loggr.

It allows you to:

  • define any combination of log levels/types;
  • create custom log types.

It supports, by default, the following levels: INFO, NOTICE, WARNING, ERROR, FATAL, DEBUG, SQL.

Getting Started

Install Tamarine in your project:

npm i tamarine

Start logging:

const logger = require('tamarine')

logger.info(logger.line())
logger.info('Hello world!')
logger.info(logger.line())

Code output:

2020-6-15 15:10:06 [INFO] ================================================================================
2020-6-15 15:10:06 [INFO] Hello world!
2020-6-15 15:10:06 [INFO] ================================================================================

Configuring Log Levels

Tamarine allows you to configure allowed levels in a simple string, joined by pipe operator, thus making simpler for working with different environments.

By default (wthout any configuration) only info, error and fatal logs will be printed in console.

Call the method below (at the very beggining of your script), passing in the desired log levels:

const logger = require('tamarine')

logger.setAllowedLogs('info|error|debug')

logger.debug('It works!')

You can also define all (all log types will be printed) or none (disable any logging) options.

Creating custom types

You can also define a custom type of log by calling this method before configuration (setAllowedLogs):

const logger = require('tamarine')

logger.setCustomLog("example")
logger.setAllowedLogs('info|error|example')

logger.custom('example', 'It works!')

Output:

2020-6-15 15:10:06 [EXAMPLE] It works!

Custom types are also subject for allowed types configuration and must be called before anything.

Logging

Docs by example:

const logger = require('tamarine')

logger.setCustomLog("test")
logger.setAllowedLogs("all") // this is needed in order to print all the messages below

logger.info(logger.line()) // prints a line inside an info message
logger.info("Info message...")
logger.notice("Notice message...")
logger.warning("Warning message...")
logger.error("Error message...")
logger.fatal("Fatal message...")
logger.debug("Debug message...")
logger.sql("SELECT something FROM example")
logger.custom("test", "A custom log type message...")
logger.info(logger.line())

Output:

2020-6-15 16:39:24 [INFO] ================================================================================
2020-6-15 16:39:24 [INFO] Info message...
2020-6-15 16:39:24 [NOTICE] Notice message...
2020-6-15 16:39:24 [WARNING] Warning message...
2020-6-15 16:39:24 [ERROR] Error message...
2020-6-15 16:39:24 [FATAL] Fatal message...
2020-6-15 16:39:24 [DEBUG] Debug message...
2020-6-15 16:39:24 [SQL] SELECT something FROM example
2020-6-15 16:39:24 [TEST] A custom log type message...
2020-6-15 16:39:24 [INFO] ================================================================================

Full Documentation

MethodDefaultDescription
setCustomLog(typeName)Allows to create a custom log type.A custom type called example will be converted to [EXAMPLE].Custom types should also be defined in setAllowedLogs in order to be printed.This method must be declared BEFORE setAllowedLogs method.
setAllowedLogs(logTypes)info|error|fatalDefines which log types will be printed.You can use any combination you want. Log types must be joined by pipe ().You can also use none to disable logging and all to automatically enable all log types.
line()Prints a line.
info(message)Prints the message with [INFO] prefix.
notice(message)Prints the message with [NOTICE] prefix.
warning(message)Prints the message with [WARNING] prefix.
error(message)Prints the message with [ERROR] prefix.
fatal(message)Prints the message with [FATAL] prefix.
debug(message)Prints the message with [DEBUG] prefix.
sql(message)Prints the message with [SQL] prefix.
custom(typeName, message)Prints the message with a custom prefix.In order to work, the custom log type must be set in setCustomLog method and defined in setAllowedLogs method.First argument is prefix and second argument is the message. Multiple custom log types are also allowed.

Contributing

  1. Fork it
  2. Download your fork to your PC (git clone https://github.com/asolera/tamarine && cd tamarine)
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Make changes and add them (git add .)
  5. Commit your changes (git commit -m 'Add some feature')
  6. Push to the branch (git push origin my-new-feature)
  7. Create new pull request

Author

Andrew Solera - andrewsolera@gmail.com