Logex
Logex is a js log module that use variables as base structure. it has instances support, coloring, and. its kinda universal through browserify. minified version is available in dist directory.
Installation:
npm install logex --save
Log instance
Options
- utc - print all times in UTC
- format {string} log format (see variables). default: {LVL}:{CONTEXT}: {ID }{MSG}
- colors {boolean} weather to use terminal colors (automatically disabled in the browser and non colorish env's)
- level - {string} default log level. defaults: production:'info', development/debugging:'debug'
- name {string} name the instance (default: 'default')
- inlineVars {boolean} - search variables syntax everywhere
*note: all of the options are optional
var logger =
Log levels (by priority):
- none
- error
- warn
- info
- debug
- verbose
Examples
var Log = ; // or window.Log in the browser // change level on the default instance Loglevel = "info"; Log; // changing log level via option declaration ; Log; // patch an object to be a logex instance Log; console; // print method bypasses the default format and allow to dynamically declare one // each % sign will pass one argument to the variable handler Log; { if !x || !y Log; return return x + y; } ; // instances var firstInstance = ; var secondInstance = ; // global declaration firstInstancevariablesTEST = 123; firstInstancevariablesSTATUS = 'GLOBAL'; // instance specific declaration firstInstanceSTATUS = 'firstInstance'; secondInstanceSTATUS = 'secondInstance'; firstInstance; secondInstance; Log;
methods
Level methods
all level methods have an single letter alias (for android addicts)
LogLogLogLogLogLog Log
Log level
default log level is: production-info, development/debugging-debug
Loglevel = 'debug'// same as
Log format
default format is: {LVL}:{CONTEXT}: {ID }{MSG}
*note: ID
variable will print nothing for the default instance
// named instances must provide their name when changing options// same as
Log#print
special method to use a custom format directly. if log level is not defined, the default one is used
print([options|level], format, ..args)
you can use the current format through Log.options.format
Log; Log; // or define custom call specific optionsLog;
Log#userWarning
alias Log.uw
user warning is meant to inform the user he misused you function. the context variable will point one call higher then itslef.
its a shortcut for
Log.print({skip: 1}, Log.options.format, "your message", "and other args")
exmaple.js
1 { 2 if !x || !y 3 Log; // CONTEXT is 'example.js:3:9' 4 Log; // CONTEXT is 'example.js:10:1' 5 return 6 7 return x + y; 8 } 9 10 ; // context
Log#patch
This will patch the console object with the selcted methods
.patch(object, methodsArray)
default methodsArray: ['error', 'warn', 'log', 'info', 'debug']
Logconsole// will print: INFO:exmaple.js:1:1 i am actually a logex method var obj = {}// will patch only the given methods even if they don't originaly exist on the target objectLogobj
Log#trace
This will print trace using the console or will print an error stack if trace method is not natively supported
Miscellaneous methods and props
These are methods and properties that exist on the logger object but generally speaking there is no reason to use them at the moment
Log // returns formatted date stringLogformats // predefined common formats (there arn't many... suggestions are welcome!)Log // reference to 'util' module methodLog // reference to 'util' module methodLogconsole // reference to the original console object
Variables
Syntax
Simple var {NAME}
Var with params {NAME params 123}
Closing var {/NAME} | {/NAME params 123}
Var with arguments {NAME %} | {NAME params 123%}
*note: never use '%' sign in parameter. use only to assign arguments.
Available variables:
{LVL}
- Log level. example: INFO, WARN, ERROR etc{LVL_LOWER}
- Log level in lower case. example: info, warn, error etc{LVL_ONE}
- Log level in a single upper case letter. example: E, W, D etc{CONTEXT 5}
- The location in which the log invoked. example: 'app.js:21:7'{MSG}
- The log message{PID}
- process.pid. example: 1725{PT}
- process.title. example: node{ENV}
- environment derived from process.env.NODE_ENV. example: 'development', 'production'' (feel free to override this or to write your own variable){ID}
- Identifier of the logger instance. the default is 'default'. Unlike any other var, it prints it paramters (not including the first space) to avoid redundant spaces when printing the var in the default logger
Date parameters:
-
{BUMP}
- time since last Bump (per instance). example: +1753ms -
{BUMP anyName FLAG}
- named bump (namepsace is per instance) with flag. empty name = 'default'.Flags:
-
START - reset start time to now and print 0ms
-
TOTAL - prints the total time from the first bump (or reset). exmaple: 7801ms, 35.4s
-
RESET - prints time diff from last bump and reset start time. exmaple: +2345ms
-
CLEAR - prints time diff from last bump and clear the namespace. this is good to avoid memory leaks
-
END - prints time diff from the last bump and clear the namespace
-
RESET_TOTAL - same as RESET but prints total time instaed
-
CLEAR_TOTAL - same is CLEAR but prints total time
-
END_TOTAL - same as END but prints total time
see Bump exmaple
-
-
{STAMP}
- timestamp. example: 1420063200000 -
{HOUR}
- time in 24 hours. example: 16:45:41.178 -
{TIME}
- time in 24 hours. example: "16:45:41 GMT-0500 (EST)" -
{UTC}
- time in 24 hours. example: Wed, 31 Dec 2015 16:45:41 GMT -
{DATE}
- Default full date. example: 31/Dec/2015:16:45:41 -0500 -
{DATE formatString}
- custom Date. see docs/DATE_FORMAT.md. example: yyyy-mm-dd'T'HH:MM:ss -> 2015-12-31T16:45:41
Colors: You can use any of the listed colors inside curly brackets
- BOLD, ITALIC, UNDERLINE, INVERSE, BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, DEFAULT, GREY, LIGHT_GARY, LIGHT_RED, LIGHT_GREEN, LIGHT_YELLOW, LIGHT_BLUE, LIGHT_MAGENTA, LIGHT_CYAN
- special color: LVL - the current level color
Example: {C LVL}[{LVL}]{/C} {C BLUE}my {C RED}red{/C} text{/C}
Default
{PT}/{PID}{LVL}:[{HOUR}]:{CONTEXT}: {MSG}
Custom variables
- Custom variables can be either global (available to all instances), or instance specific
- variables name must be in UPPER case. _ and $ are allowed.
- Variable value is converted to string. function is invoked first.
- params should never contain the '%' sign as it reserved for argument assignment
- If you want to use the same function with several instances you can save data on Log.data {object} this object always starts empty.
- variable name is available on state.name
- synchronous!
Global declaration
// StaticLogvariablesSTATUS = 'Listening'; var obj = status: 'Listening'// object reference - practically dynamicLogvariablesSTATUS = obj; // functionLogvariables { // this points the current instance (the one that used the variable); thisdatamyData = 'per instance safe space to store data'; return thisname + ':' + }
Instance declaration:
// StaticLogSTATUS = 'Listening'; // DynamicLog { // this points the current instance return thisname + ':' + }
Passing arguments to custom Variables:
// define custom variable. 4th argument and up passed by the caller Logvariables { return extraArg1method + ' ' + extraArg1url; }; // simulate request object var httpRequest = url: '/home' method: 'GET' ; // use % to pass 1 argument Log; // will print: [D] GET /home
Log; ; ; ; ; ;
will print:
#1 total:0ms diff: +0ms
#2 total:1237ms diff: +1237ms
#3(reset) total:2438ms diff: +1201ms
#4 total:850ms diff: +850ms counted relative to last call
#5(clear) total:1950ms diff: +1100ms clear the counter
#6 total:0ms diff: +0ms passively starts the timer again