├── .gitignore ├── LICENSE ├── README.md ├── example.js ├── logger.js ├── package.json └── snapshots └── basic.png /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/* 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (The MIT License) 2 | 3 | Copyright (c) 2012 Felix Milea-Ciobanu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | 'Software'), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # custom-logger [![npm version](https://badge.fury.io/js/custom-logger.svg)](http://badge.fury.io/js/custom-logger) 2 | 3 | `custom-logger` is a simple, highly customizable logging plugin for node.js. 4 | 5 | To install this module simply run: 6 | 7 | ```bash 8 | npm install custom-logger 9 | ``` 10 | 11 | I'm still very much still learning node.js, and this is my first "official" module. If you notice any unusual or depracated coding behaviors be sure to let me know, like I said, I'm still learning! (: 12 | 13 | If you have any questions or feedback, or need any help using this module, please contact @felix_mc. 14 | 15 | 16 | ## Basic Usage 17 | 18 | ```javascript 19 | var log = require('custom-logger').config({ level: 0 }); 20 | 21 | log.debug( 'hello?' ); 22 | 23 | log.info( 'hello world!' ); 24 | 25 | log.warn( 'carefule there, world!' ); 26 | 27 | log.error( 'WHOA WHOA WHOA world?!' ); 28 | 29 | log.config({ 30 | messageFormatting: true 31 | }); 32 | 33 | log.info('I see %d %s!', 3, 'bananas'); 34 | ``` 35 | 36 | The above code will render to: 37 | 38 | ![basic output snapshot](snapshots/basic.png) 39 | 40 | `custom-logger` uses logging levels and different event types to determine what is being outputed at any given time. Below are the default events and their respective logging levels: 41 | 42 | 0 => debug 43 | 1 => info 44 | 2 => warn 45 | 3 => error 46 | 47 | As you can see above, you can use the level parameter in the config method to set the current logging level. You can call on the config method on the log object at any time throughout your code to change the log level or other options. 48 | 49 | `custom-log` will only show events whose level is **equal to or higher** than the one set. For example a level of `0` will show all event types, whereas a level of `2` will only show the event types `warning` and `error`. By default the logging level is `0`. 50 | 51 | You can pass any number of parameters to logging methods (like log.info() or log.error), regardless of whether they are a default method or one that you created (see below). custom-logger will join all parameters together in one string, converting JSON and other non-string parameters to strings automatically. 52 | 53 | ## Customizing Events Types 54 | 55 | One cool thing you can do with `custom-logger` is you easily create your own additional event types: 56 | 57 | ```javascript 58 | log.new({ 59 | fatal: { level: 5, event: 'fatal', color: 'rainbow' } 60 | }); 61 | 62 | log.fatal( 'program is dead!' ); //outputs "fatal: program is dead" 63 | ``` 64 | 65 | In the parameters passed to the `new` method, the key (`debugger`) is the method to be added to the module, where as the `event` property is what is going to be displayed in the log as the event type. Don't the get two confused! 66 | 67 | You can also overwrite the default events (`info`, `warn`, `error`) with your own if you'd like to: 68 | 69 | ```javascript 70 | log.new({ 71 | info: { color: 'cyan', level: 0, event: 'info' }, 72 | notice: { color: 'yellow', level: 1, event: 'notice' }, 73 | warn: { color: 'yellow', level: 2, event: 'warning' }, 74 | error: { color: 'red', level: 3, event: 'ERROR' } 75 | }); 76 | ``` 77 | 78 | However, if all you you want to do is change some of the properties, you can also do the following: 79 | 80 | ```javascript 81 | log.info().config({ color: 'cyan' }); 82 | 83 | log.info('Hello World!'); //output should be cyan now 84 | ``` 85 | 86 | Make sure not to pass anything to the `info` method when you are configuring it, otherwise you'll probably get an error. This is the only *ugly* part of the syntax (hopefully!), I will probably fix it at a later time. 87 | 88 | ## Log Formatting 89 | 90 | ### Template 91 | 92 | Using `custom-logger` you can also configure how the console output will look like. It uses some simple templating patterns to do so: 93 | 94 | %event% => the type of event being logged, such as "info" or "warning" 95 | %message% => the message to be displayed, such as "server started!" 96 | %timestamp% => customizable datetime (see below) for when the event occured 97 | %padding% => space characters that help align the current %event%. See details sections below for more 98 | 99 | By default the format string is: 100 | 101 | %timestamp% - %event%:%padding% %message% 102 | 103 | Which produces the output: 104 | 105 | 01:08:24 - debug: hello? 106 | 01:08:24 - info: hello world! 107 | 01:08:24 - warning: carefule there, world! 108 | 01:08:24 - error: WHOA WHOA WHOA world?! 109 | 110 | You can specify the output format like so: 111 | 112 | ```javascript 113 | var log = require('custom-logger').config({ format: '%event% %padding%[%timestamp%]: %message%' }); 114 | ``` 115 | 116 | Which would output to: 117 | 118 | debug [01:06:26]: hello? 119 | info [01:06:26]: hello world! 120 | warning [01:06:26]: carefule there, world! 121 | error [01:06:26]: WHOA WHOA WHOA world?! 122 | 123 | If you were to remove `%padding%` from the format string, the output would be: 124 | 125 | debug [01:10:12]: hello? 126 | info [01:10:12]: hello world! 127 | warning [01:10:12]: carefule there, world! 128 | error [01:10:12]: WHOA WHOA WHOA world?! 129 | 130 | I hope this helps illustrate the purpose of `%padding%`. 131 | 132 | ### Timestamp 133 | 134 | You can also modify the format of the actual `%timestamp%`. By default it's `HH:MM:ss`. You can change it as follows: 135 | 136 | ```javascript 137 | var log = require('custom-logger').config({ timestamp: 'h:MM:ss TT' }); 138 | ``` 139 | 140 | Which should look familiar to those familiar with date formatting in other programming languages. For more information on formatting the timestamp, see this. Since this modules relies on the `dateFormat` library, any values valid for `dateFormat` should be valid here too. 141 | 142 | ## Overwriting Global Config 143 | 144 | You can also overwrite global settings for log template and timestamp format based on individual event types, either when you create them or by modifying existing ones: 145 | 146 | ```javascript 147 | log.new({ 148 | alert: { level: 3, color: 'red', event: 'alert', format: '!!!!!%event% : %message% !!!!!' } 149 | }); 150 | 151 | log.error().config({ timestamp: 'HH:MM:ss' }); 152 | ``` 153 | 154 | ## Further Details 155 | 156 | ### Colors 157 | 158 | If you'd like to further customize colors or formatting of your events, take a look at the colors modules that this module depends on. Any color/formatting value that's valid for that module, should be fine to pass to the `color` parameter when creating/configuring an event type. 159 | 160 | ### Padding 161 | 162 | Say you have three event types with the `event` properties as follows: `info`, `warning`, `error`. 163 | 164 | The `%padding%` value is going to be different for each of the events when they are displayed. The way it's calculated is that it finds the string length of the longest event, and subtracts it from the string length of the current event, and then returns a string containg that many spaces. 165 | 166 | For examples, the longest string of the above events is `warning`, with 7 characters. Here's how it's being worked out: 167 | 168 | info.length = 4 ===> 7 - 4 = 3 ===> " " 169 | warning.length = 7 ===> 7 - 7 = 0 ===> "" 170 | error.length = 5 ===> 7 - 5 = 2 ===> " " 171 | 172 | If this confuses you, don't worry much about it, you don't have to use it. I simply included it to make outputing to the console prettier. 173 | 174 | ### Message Formatting 175 | 176 | Messages can be formatted using `util.format` by setting the config option `messageFormatting` to `true`. This defaults to `false` to ensure backward compatibility with versions prior to 0.3.1. 177 | 178 | For example: 179 | 180 | ```javascript 181 | log.info('I see', '3', 'bananas!') // Outputs "I see 3 bananas!" 182 | 183 | log.config({ 184 | messageFormatting: true 185 | }); 186 | 187 | log.info('I see %d %s!', 3, 'bananas'); // Outputs "I see 3 bananas!" 188 | ``` 189 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var log = require('./logger').config({ level: 0 }); 2 | 3 | log.debug('hello?'); 4 | 5 | log.info('hello world!'); 6 | 7 | log.warn('careful there, world'); 8 | 9 | log.error('WHOA WHOA WHOA world!'); 10 | 11 | log.info("I see", "3", "bananas!") // Outputs "I see 3 bananas!" 12 | 13 | log.config({ 14 | messageFormatting: true 15 | }); 16 | 17 | log.info("I see %d %s!", 3, "bananas"); // Outputs "I see 3 bananas!" 18 | -------------------------------------------------------------------------------- /logger.js: -------------------------------------------------------------------------------- 1 | var dateFormat = require('dateformat'), 2 | colors = require('colors'), 3 | util = require('util'); 4 | 5 | var events = { }; 6 | 7 | var options = { 8 | level: 0, 9 | format: "%timestamp% - %event%:%padding% %message%", 10 | timestamp: "HH:MM:ss", 11 | messageFormatting: false 12 | }; 13 | 14 | function log_event( options ) { 15 | this.event = options.event; 16 | this.level = options.level || 0; 17 | this.color = options.color || 'white'; 18 | }; 19 | 20 | log_event.prototype.config = function( config ) { 21 | for(var key in config) { 22 | this[key] = config[key]; 23 | } 24 | return this; 25 | } 26 | 27 | log_event.prototype.__defineGetter__ ('padding', function() { 28 | var length = 0, 29 | padding = ''; 30 | for(var key in events) { 31 | if(events.hasOwnProperty(key)) 32 | length = length < events[key].event.length ? events[key].event.length : length; 33 | } 34 | for(var i=0;i (http://felixmc.com/)", 6 | "repository": { 7 | "type": "git", 8 | "url": "http://github.com/felixmc/custom-logger.git" 9 | }, 10 | "main": "logger", 11 | "engines": { 12 | "node": ">=0.1.90" 13 | }, 14 | "dependencies": { 15 | "colors": "^1.1.0", 16 | "dateformat": "1.0.2-1.2.3" 17 | }, 18 | "licenses": [ 19 | { 20 | "type": "MIT", 21 | "url": "http://www.opensource.org/licenses/MIT" 22 | } 23 | ], 24 | "keywords": [ 25 | "log", 26 | "logger", 27 | "logging", 28 | "color", 29 | "colour", 30 | "timestamp", 31 | "custom", 32 | "console", 33 | "warning", 34 | "error", 35 | "debug", 36 | "template" 37 | ] 38 | } 39 | -------------------------------------------------------------------------------- /snapshots/basic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/felixmc/custom-logger/cf2c7e09b472775e0bb4c78c6a2d213d45e99952/snapshots/basic.png --------------------------------------------------------------------------------