├── package.json ├── bower.json ├── .gitignore ├── .gitattributes ├── LICENSE ├── Gruntfile.js ├── README.md └── logger.service.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@iamadamjowett/angular-logger-max", 3 | "version": "1.2.3", 4 | "author": { 5 | "name": "Adam Jowett", 6 | "email": "akuma.me@gmail.com" 7 | }, 8 | "repository": { 9 | "type": "git", 10 | "url": "git@github.com:IamAdamJowett/angular-logger-max.git" 11 | }, 12 | "bugs": { 13 | "url": "https://github.com/IamAdamJowett/angular-logger-max/issues" 14 | }, 15 | "license": "MIT", 16 | "devDependencies": { 17 | "grunt": "^0.4.5", 18 | "grunt-bump": "^0.3.2" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "angular-logger-max", 3 | "version": "1.2.3", 4 | "main": "logger.service.js", 5 | "homepage": "https://github.com/IamAdamJowett/angular-logger-max", 6 | "authors": [ 7 | "IamAdamJowett" 8 | ], 9 | "description": "A custom logger for angular with color coding and console.re support.", 10 | "keywords": [ 11 | "angular", 12 | "console", 13 | "logs", 14 | "logger" 15 | ], 16 | "license": "MIT", 17 | "ignore": [], 18 | "repository": { 19 | "type": "git", 20 | "url": "git://github.com/IamAdamJowett/angular-logger-max" 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Laravel 2 | /bootstrap/compiled.php 3 | .env.*.php 4 | .env.php 5 | 6 | # PUPHET 7 | /puphpet/* 8 | !/puphpet/config.yaml 9 | !/puphpet/README.md 10 | 11 | # Sublime Text IDE 12 | *.tmlanguage.cache 13 | *.tmPreferences.cache 14 | *.stTheme.cache 15 | *.sublime-workspace 16 | *.sublime-project 17 | sftp-config.json 18 | 19 | # MISC 20 | /vendor 21 | composer.phar 22 | .DS_Store 23 | Thumbs.db 24 | _notes 25 | .vagrant 26 | /*.lnk 27 | /bower_components 28 | /node_modules 29 | 30 | # Brackets IDE 31 | .ftppass 32 | .brackets.json 33 | .grunt* 34 | 35 | # PhpStorm IDE 36 | .idea 37 | 38 | # Binaries 39 | *.exe 40 | *.app 41 | *.rar 42 | *.zip -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | # http://davidlaing.com/2012/09/19/customise-your-gitattributes-to-become-a-git-ninja/ 3 | * text=auto 4 | 5 | # 6 | # The above will handle all files NOT found below 7 | # 8 | 9 | # These files are text and should be normalized 10 | *.php text 11 | *.css text 12 | *.js text 13 | *.htm text 14 | *.html text 15 | *.xml text 16 | *.txt text 17 | *.ini text 18 | *.inc text 19 | *.md text 20 | .htaccess text 21 | 22 | 23 | # These files are binary and should be left untouched 24 | # (binary is a macro for -text -diff) 25 | *.png binary 26 | *.jpg binary 27 | *.jpeg binary 28 | *.gif binary 29 | *.ico binary 30 | *.mov binary 31 | *.mp4 binary 32 | *.mp3 binary 33 | *.flv binary 34 | *.fla binary 35 | *.swf binary 36 | *.gz binary 37 | *.zip binary 38 | *.7z binary 39 | *.ttf binary 40 | 41 | # Documents 42 | *.doc diff=astextplain 43 | *.DOC diff=astextplain 44 | *.docx diff=astextplain 45 | *.DOCX diff=astextplain 46 | *.dot diff=astextplain 47 | *.DOT diff=astextplain 48 | *.pdf diff=astextplain 49 | *.PDF diff=astextplain 50 | *.rtf diff=astextplain 51 | *.RTF diff=astextplain -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Adam Jowett 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Gruntfile.js: -------------------------------------------------------------------------------- 1 | module.exports = function(grunt) { 2 | 'use strict'; 3 | 4 | grunt.initConfig({ 5 | pkg: grunt.file.readJSON('package.json'), 6 | bump: { 7 | options: { 8 | files: ['package.json', 'bower.json'], 9 | updateConfigs: [], 10 | commit: true, 11 | commitMessage: 'Bumped to release v%VERSION%', 12 | commitFiles: ['package.json', 'bower.json'], 13 | createTag: true, 14 | tagName: 'v%VERSION%', 15 | tagMessage: 'Version %VERSION%', 16 | push: false, 17 | pushTo: 'upstream', 18 | gitDescribeOptions: '--tags --always --abbrev=1 --dirty=-d', 19 | globalReplace: false, 20 | prereleaseName: false, 21 | regExp: false 22 | } 23 | } 24 | }); 25 | 26 | grunt.loadNpmTasks('grunt-bump'); 27 | 28 | /**\/ 29 | $ grunt bump 30 | >> Version bumped to 0.0.2 31 | >> Committed as "Release v0.0.2" 32 | >> Tagged as "v0.0.2" 33 | >> Pushed to origin 34 | 35 | $ grunt bump:patch 36 | >> Version bumped to 0.0.3 37 | >> Committed as "Release v0.0.3" 38 | >> Tagged as "v0.0.3" 39 | >> Pushed to origin 40 | 41 | $ grunt bump:minor 42 | >> Version bumped to 0.1.0 43 | >> Committed as "Release v0.1.0" 44 | >> Tagged as "v0.1.0" 45 | >> Pushed to origin 46 | 47 | $ grunt bump:major 48 | >> Version bumped to 1.0.0 49 | >> Committed as "Release v1.0.0" 50 | >> Tagged as "v1.0.0" 51 | >> Pushed to origin 52 | 53 | $ grunt bump:patch 54 | >> Version bumped to 1.0.1 55 | >> Committed as "Release v1.0.1" 56 | >> Tagged as "v1.0.1" 57 | >> Pushed to origin 58 | 59 | $ grunt bump:git 60 | >> Version bumped to 1.0.1-ge96c 61 | >> Committed as "Release v1.0.1-ge96c" 62 | >> Tagged as "v1.0.1-ge96c" 63 | >> Pushed to origin 64 | 65 | $ grunt bump:prepatch 66 | >> Version bumped to 1.0.2-0 67 | >> Committed as "Release v1.0.2-0" 68 | >> Tagged as "v1.0.2-0" 69 | >> Pushed to origin 70 | 71 | $ grunt bump:prerelease 72 | >> Version bumped to 1.0.2-1 73 | >> Committed as "Release v1.0.2-1" 74 | >> Tagged as "v1.0.2-1" 75 | >> Pushed to origin 76 | 77 | $ grunt bump:patch # (major, minor or patch) will do this 78 | >> Version bumped to 1.0.2 79 | >> Committed as "Release v1.0.2" 80 | >> Tagged as "v1.0.2" 81 | >> Pushed to origin 82 | 83 | $ grunt bump:preminor 84 | >> Version bumped to 1.1.0-0 85 | >> Committed as "Release v1.1.0-0" 86 | >> Tagged as "v1.1.0-0" 87 | >> Pushed to origin 88 | 89 | $ grunt bump 90 | >> Version bumped to 1.1.0 91 | >> Committed as "Release v1.1.0" 92 | >> Tagged as "v1.1.0" 93 | >> Pushed to origin 94 | 95 | $ grunt bump:premajor (with prerelaseName set to 'rc' in options) 96 | >> Version bumped to 2.0.0-rc.0 97 | >> Committed as "Release v2.0.0-rc.0" 98 | >> Tagged as "v2.0.0-rc.0" 99 | >> Pushed to origin 100 | 101 | $ grunt bump 102 | >> Version bumped to 2.0.0 103 | >> Committed as "Release v2.0.0" 104 | >> Tagged as "v2.0.0" 105 | >> Pushed to origin 106 | 107 | $ grunt bump:prerelease # from a released version `prerelease` defaults to prepatch 108 | >> Version bumped to 2.0.1-rc.0 109 | >> Committed as "Release v2.0.1-rc.0" 110 | >> Tagged as "v2.0.1-rc.0" 111 | >> Pushed to origin 112 | /**/ 113 | }; 114 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # angular-logger-max 2 | A custom logger for angular with color coding (in Chrome, plain in other browsers including Internet Explorer) and also with remote debugging options via the [console.re](console.re) service. 3 | 4 | The benefits of using this logger instead of vanilla console.log outputs is readability, extra information by default and the ability to turn all debugging on or off via `Logger.debug = false` or `Logger.debug = true`; 5 | 6 | The exception to the above are errors and warnings which are always outputted to the console. 7 | 8 | There are options to show the full stack in an output as well as whether to expand and print out objects by default on a per call basis. 9 | 10 | As an example, here is a screenshot showing some of the different types of Logger method outputs: 11 | 12 | ![browser console](https://cloud.githubusercontent.com/assets/294948/9345972/206910f2-465a-11e5-85a6-9f1b9e0365b1.png) 13 | 14 | ## Installation 15 | 16 | There are two easy ways to install the Logger service: 17 | 18 | ####npm 19 | 20 | npm install @iamadamjowett/angular-logger-max 21 | 22 | #### With Bower 23 | 24 | To install via Bower, run: 25 | 26 | bower install angular-logger-max 27 | 28 | #### Manual Installation 29 | 30 | Download the `logger.service.js` file, and include it in your index.html file with something like: 31 | 32 | 33 | 34 | Also be sure to include the module in your app.js file with: 35 | 36 | angular.module('yourAppName', ['angular-logger-max']); 37 | 38 | ## Usage 39 | 40 | All Logger methods take the same parameters, all if which are option except for 'prepend', which if it is the only parameter passed, turns into the log message: 41 | 42 | ###Parameters 43 | 44 | - prepend - A string to prepend your output with, useful when labelling output of an object such as Logger.log('myObj: ', myObj); 45 | - msg - The main output string (if prepend is not passed, then prepend becomes the same as this). Can be of any type. 46 | - fullStack - A boolean indicating whether to output the entire stack trace (if it is gettable in the browser) for more detailed debugging (default false) 47 | - expand - A boolean indicating whether to expand objects by default via JSON.stringify (default false) 48 | 49 | ```` 50 | Logger.log('This is a plain old boring log, but it will be colorful'); 51 | Logger.info('An object, but not via Logger.data, expanded: ', someObj, false, true); 52 | Logger.shout('This is like a log, but loud, and stands out in the console'); 53 | Logger.data('myObject structure: ', myObject); 54 | Logger.error('An error, so lets see the full stack trace', null, true, false); 55 | Logger.loaded('This controller has loaded, it is the same as a plain log just in a different colour'); 56 | Logger.track('An event has been sent to tracking, this is like a plain log but muted down'); 57 | ```` 58 | 59 | ## Output 60 | 61 | ### Colours 62 | logger-max has been designed for the Chome debugger, but will work in all major browsers. The colouring features at the moment are a Chrome only feature, with other browsers degrading gracefully depending on available features. 63 | 64 | ### Module types 65 | If you follow the naming convention of foo.controller.js or bar.directive.js for your different angular module types, logger-max will pick this up and output them in square brackets after the log type: 66 | 67 | ![angular module](https://cloud.githubusercontent.com/assets/294948/9346156/b0d1e108-465c-11e5-8b0e-699010e3898f.png) 68 | 69 | ## Logging to the http://console.re service 70 | 71 | Integration into the remote logging service at console.re is available by default. To enable this feature, simply go to the [console.re](console.re) website and follow their "How to install" instructions. 72 | 73 | angular-logger-max will automatically look to see if the library has been included, and if so, will log out to both the local console, and to the console.re console you have set up. 74 | 75 | An example output looks something like this in the console.re console: 76 | 77 | ![console.re output](https://cloud.githubusercontent.com/assets/294948/9345973/206bcfb8-465a-11e5-8331-fede2190d5df.png) 78 | 79 | When console.re is included, you will get duplicate logs in your browser console, to clean this up, you can filter by [#] to show only non-console.re logs: 80 | 81 | ![filter](https://cloud.githubusercontent.com/assets/294948/9345974/206c588e-465a-11e5-8ac5-da09ae1b0561.png) 82 | -------------------------------------------------------------------------------- /logger.service.js: -------------------------------------------------------------------------------- 1 | /* global angular */ 2 | 3 | (function() { 4 | 'use strict'; 5 | 6 | angular 7 | .module('angular-logger-max', []) 8 | .factory('Logger', [Logger]); 9 | 10 | // check for the availablility of the variety of console functions and create holders if necessary 11 | (function() { 12 | // handles all the console methods to make the console persistent, mainly for IE 13 | if (window.console === undefined) { 14 | window.console = {}; 15 | } 16 | 17 | // assign holder functions to any console method not available to avoid old browser errors 18 | (function() { 19 | var methods = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"], 20 | i = methods.length; 21 | for (; i--;) { 22 | if (!window.console[methods[i]]) { 23 | window.console[methods[i]] = function() {}; 24 | } 25 | } 26 | }()); 27 | }()); 28 | 29 | /** 30 | * @ngdoc service 31 | * @module angular-logger-max 32 | * @name angular-logger-max.service:Logger 33 | * @description Service to provide smarter and more descriptive logs to the console and remote logging services 34 | */ 35 | function Logger() { 36 | var _debug = false, 37 | _api = { 38 | log: log, 39 | error: error, 40 | warn: warn, 41 | info: info, 42 | data: data, 43 | shout: shout, 44 | loaded: loaded, 45 | track: track, 46 | get debug() { 47 | return _debug; 48 | }, 49 | set debug(val) { 50 | _debug = val; 51 | } 52 | }; 53 | 54 | return _api; 55 | 56 | /** 57 | * @ngdoc method 58 | * @name log 59 | * @description Method to log out a message or object along the lines of console.log 60 | * @methodOf angular-logger-max.service:Logger 61 | * @param {*} prepend A string or object to prepend the message with 62 | * @param {*} msg The message to display 63 | * @param {Boolean} fullStack Whether to show the full stack with the message 64 | * @param {Boolean} expand Whether to expand the structure of any objects or arrays logged out 65 | * @return {*} The string or object to log to the console 66 | */ 67 | function log(prepend, msg, fullStack, expand) { 68 | if (!_debug) return; 69 | 70 | _formatOutput('log', 'color: green', prepend, msg, _formatStackTrace(fullStack), expand); 71 | 72 | if (console.re) { 73 | console.re.log(prepend, (msg) ? msg : '<< END'); 74 | } 75 | } 76 | 77 | /** 78 | * @ngdoc method 79 | * @name info 80 | * @description Method to log out a message or object along the lines of console.info 81 | * @methodOf angular-logger-max.service:Logger 82 | * @param {*} prepend A string or object to prepend the message with 83 | * @param {*} msg The message to display 84 | * @param {Boolean} fullStack Whether to show the full stack with the message 85 | * @param {Boolean} expand Whether to expand the structure of any objects or arrays logged out 86 | * @return {*} The string or object to log to the console 87 | */ 88 | function info(prepend, msg, fullStack, expand) { 89 | if (!_debug) return; 90 | 91 | _formatOutput('info', 'color: blue', prepend, msg, _formatStackTrace(fullStack), expand); 92 | 93 | if (console.re) { 94 | console.re.info(prepend, (msg) ? msg : '<< END'); 95 | } 96 | } 97 | 98 | /** 99 | * @ngdoc method 100 | * @name warn 101 | * @description Method to log out a message or object along the lines of console.warn 102 | * @methodOf angular-logger-max.service:Logger 103 | * @param {*} prepend A string or object to prepend the message with 104 | * @param {*} msg The message to display 105 | * @param {Boolean} fullStack Whether to show the full stack with the message 106 | * @param {Boolean} expand Whether to expand the structure of any objects or arrays logged out 107 | * @return {*} The string or object to log to the console 108 | */ 109 | function warn(prepend, msg, fullStack, expand) { 110 | // always show warnings, debug or not 111 | _formatOutput('warn', 'color: orange', prepend, msg, _formatStackTrace(fullStack), expand); 112 | 113 | if (console.re) { 114 | console.warn(prepend, (msg) ? msg : '<< END'); 115 | } 116 | } 117 | 118 | /** 119 | * @ngdoc method 120 | * @name error 121 | * @description Method to log out a message or object along the lines of console.error 122 | * @methodOf angular-logger-max.service:Logger 123 | * @param {*} prepend A string or object to prepend the message with 124 | * @param {*} msg The message to display 125 | * @param {Boolean} fullStack Whether to show the full stack with the message (defaults to true) 126 | * @param {Boolean} expand Whether to expand the structure of any objects or arrays logged out 127 | * @return {*} The string or object to log to the console 128 | */ 129 | function error(prepend, msg, fullStack, expand) { 130 | fullStack = typeof fullStack === 'undefined' ? true : fullStack; 131 | 132 | // always show errors, debug or not 133 | _formatOutput('error', 'background-color: maroon; font-weight: bold; color: white', prepend, msg, _formatStackTrace(fullStack), expand); 134 | 135 | if (console.re) { 136 | console.re.error(prepend, (msg) ? msg : '<< END'); 137 | console.re.trace(); 138 | } 139 | } 140 | 141 | /** 142 | * @ngdoc method 143 | * @name data 144 | * @description Method to log out the structure of an object or array 145 | * @methodOf angular-logger-max.service:Logger 146 | * @param {*} prepend A string or object to prepend the message with 147 | * @param {*} msg The message to display 148 | * @param {Boolean} fullStack Whether to show the full stack with the message 149 | * @param {Boolean} expand Whether to expand the structure of any objects or arrays logged out (defaults to true) 150 | * @return {*} The string or object to log to the console 151 | */ 152 | function data(prepend, msg, fullStack, expand) { 153 | if (!_debug) return; 154 | 155 | // for data, by default log them out as full objects 156 | expand = typeof expand === 'undefined' ? true : expand; 157 | 158 | _formatOutput('data', 'color: hotpink', prepend, msg, _formatStackTrace(fullStack), expand); 159 | 160 | if (console.re) { 161 | console.re.debug(prepend, (msg) ? msg : '<< END'); 162 | } 163 | } 164 | 165 | /** 166 | * @ngdoc method 167 | * @name shout 168 | * @description Method to log out a message in a way to make the message more noticable in the console 169 | * @methodOf angular-logger-max.service:Logger 170 | * @param {*} prepend A string or object to prepend the message with 171 | * @param {*} msg The message to display 172 | * @param {Boolean} fullStack Whether to show the full stack with the message 173 | * @param {Boolean} expand Whether to expand the structure of any objects or arrays logged out 174 | * @return {*} The string or object to log to the console 175 | */ 176 | function shout(prepend, msg, fullStack, expand) { 177 | if (!_debug) return; 178 | 179 | _formatOutput('shout', 'color: red; font-weight: bold; font-size: 125%;', prepend, msg, _formatStackTrace(fullStack), expand); 180 | 181 | if (console.re) { 182 | console.re.log(prepend, (msg) ? msg : '<< END'); 183 | } 184 | } 185 | 186 | /** 187 | * @ngdoc method 188 | * @name loaded 189 | * @description Method to log out a message in a different colour than a standard log 190 | * @methodOf angular-logger-max.service:Logger 191 | * @param {*} prepend A string or object to prepend the message with 192 | * @param {*} msg The message to display 193 | * @param {Boolean} fullStack Whether to show the full stack with the message 194 | * @param {Boolean} expand Whether to expand the structure of any objects or arrays logged out 195 | * @return {*} The string or object to log to the console 196 | */ 197 | function loaded(prepend, msg, fullStack, expand) { 198 | if (!_debug) return; 199 | 200 | _formatOutput('loaded', 'color: purple', prepend, msg, _formatStackTrace(fullStack), expand); 201 | 202 | if (console.re) { 203 | console.re.log(prepend, (msg) ? msg : '<< END'); 204 | } 205 | } 206 | 207 | /** 208 | * @ngdoc method 209 | * @name track 210 | * @description Method to log out a message in a muted colour than a standard log 211 | * @methodOf angular-logger-max.service:Logger 212 | * @param {*} prepend A string or object to prepend the message with 213 | * @param {*} msg The message to display 214 | * @param {Boolean} fullStack Whether to show the full stack with the message 215 | * @param {Boolean} expand Whether to expand the structure of any objects or arrays logged out 216 | * @return {*} The string or object to log to the console 217 | */ 218 | function track(prepend, msg, fullStack, expand) { 219 | if (!_debug) return; 220 | 221 | _formatOutput('tracking', 'color: grey', prepend, msg, _formatStackTrace(fullStack), expand); 222 | 223 | if (console.re) { 224 | console.re.log(prepend, (msg) ? msg : '<< END'); 225 | } 226 | } 227 | 228 | /** 229 | * @description Function to format the console outputs according to what types of things are passed 230 | * @param {String} type A string to indicate the type of log 231 | * @param {String} styles A string of css styles 232 | * @param {String} prepend Text to prepend the output with 233 | * @param {Mixed} msg What is to be outputted to the console 234 | * @param {Boolean} fullStack Indicate whether the full stack trace should be shown (false by default) 235 | * @param {Boolean} expandObj Indicate whether any object being logged should be expanded in string form (false by default) 236 | */ 237 | function _formatOutput(type, styles, prepend, msg, fullStack, expandObj, remote) { 238 | var stackString = _trace() || '[unknown]', 239 | moduleType; 240 | 241 | fullStack = fullStack || ''; 242 | 243 | // pre-process type according to calling function 244 | moduleType = fullStack.substring(fullStack.indexOf('.', 0) + 1, _xIndexOf('.', fullStack, 2)); 245 | type = (_xIndexOf('.', fullStack, 2) > 0) ? ((stackString) ? '%c' : '') + '[#][' + type.toUpperCase() + '][' + _toTitleCase(moduleType) + '] ' : ((stackString) ? '%c' : '') + '[#][' + type.toUpperCase() + '] '; 246 | 247 | if (msg === undefined && typeof prepend !== 'object' && typeof prepend !== 'function') { // if a plain string 248 | if (stackString && stackString.length > 0) { 249 | console.log(type, styles, prepend, fullStack); 250 | } 251 | else { 252 | console.log(type, prepend, fullStack); 253 | } 254 | } 255 | else if (typeof prepend === 'object') { // if a single object with no prepending text 256 | if (expandObj) { 257 | if (stackString.length > 0) { 258 | console.log(type + JSON.stringify(prepend, null, '\t'), styles, fullStack); 259 | } 260 | else { 261 | console.log(type + JSON.stringify(prepend, null, '\t')); 262 | } 263 | } 264 | else { 265 | if (stackString.length > 0) { 266 | console.log(type, styles, prepend, fullStack); 267 | } 268 | else { 269 | console.log(type, prepend, fullStack); 270 | } 271 | } 272 | } 273 | else { // if prepend and msg exists 274 | if (typeof msg === 'object' && expandObj) { // if msg is an object and it needs to be automatically expanded 275 | if (stackString.length > 0) { 276 | console.log(type + prepend, styles, JSON.stringify(msg, null, '\t'), fullStack); 277 | } 278 | else { 279 | console.log(type + prepend, JSON.stringify(msg, null, '\t'), fullStack); 280 | } 281 | } 282 | else { // log it out as per normal 283 | if (stackString.length > 0) { 284 | console.log(type + prepend, styles, msg, fullStack); 285 | } 286 | else { 287 | console.log(type + prepend, msg, fullStack); 288 | } 289 | } 290 | } 291 | } 292 | 293 | /** 294 | * @description get the stack track from the output of a dummy error message so we can provide meaningful path information 295 | */ 296 | function _trace() { 297 | var err = new Error(); 298 | return err.stack; 299 | } 300 | 301 | /** 302 | * @description Function to format the full or summary stack trace to the console 303 | * @param {Boolean} fullStack Indicate whether the full stack should be shown in the console or just the filename 304 | * @return {String} 305 | */ 306 | function _formatStackTrace(fullStack) { 307 | fullStack = typeof fullStack === 'undefined' ? false : fullStack; 308 | 309 | if (!fullStack) { 310 | var lines = (_trace()) ? _trace().split('\n') : '', 311 | i, 312 | l; 313 | 314 | for (i = 0; i < lines.length; i++) { 315 | var val = lines[i]; 316 | if (val.toString() 317 | .indexOf('logger.js') === -1 && val.toString() !== 'Error') { 318 | return ('____ [' + lines[4].substring(lines[4].lastIndexOf('/') + 1) + ']') 319 | .replace(')', ''); 320 | } 321 | } 322 | } 323 | 324 | return (console.re) ? console.re.trace() : _trace(); 325 | } 326 | 327 | /** 328 | * @description Function to return the 2nd, 3rd or nth instance of a needle in a string 329 | * @usage var PicPath = "/somedirectory/pics/"; 330 | * var AppPath = picpath.substring(0, xIndexOf('/', PicPath, 2) + 1); 331 | * @param {Number} instance the number instance to find 332 | * @param {String} needle the needle to search for 333 | * @param {String} haystack the string to search within 334 | * @return {Number} the position in the string the nth instance of the needle was found in 335 | */ 336 | function _xIndexOf(needle, haystack, instance) { 337 | var found, 338 | i; 339 | 340 | if (instance <= (haystack.split(needle).length - 1)) { 341 | found = haystack.indexOf(needle); 342 | if (instance > 1) { 343 | for (i = 1; i < instance; i++) { 344 | found = haystack.indexOf(needle, found + 1); 345 | } 346 | } 347 | return found; 348 | } 349 | else { 350 | return 0; 351 | } 352 | } 353 | 354 | /** 355 | * Function to title case a string 356 | * @param {String} str the string to title case 357 | * @return {String} 358 | */ 359 | function _toTitleCase(str) { 360 | return str.replace(/\w\S*/g, function(txt) { 361 | return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase(); 362 | }); 363 | } 364 | } 365 | })(); 366 | --------------------------------------------------------------------------------