Click here to Skip to main content
13,558,356 members
Click here to Skip to main content
Add your own
alternative version

Stats

9.6K views
2 bookmarked
Posted 15 Sep 2013
Licenced CPOL

How to Alter console.log

, 31 Oct 2013
Rate this:
Please Sign up or sign in to vote.
Adding stuff to your logs.

Introduction

If you need to alter Node.JS server or JavaScript client console.log function w/o the vsprintf library, dynamically build the util.format command and execute with JavaScript eval().

Using the Code

Place the following code in your file to replace the default console.log function:

util = require('util');
console.log = function(a) {
    if (arguments.length>1) {
        var cmd = 'util.format(';
        for (i in arguments) {
            if (i>0)
                cmd += ',';
            cmd += 'arguments['+i+']';
        }
        cmd += ')';
        util.log(eval(cmd));
    }
    else
        util.log(a);
};

This is much better:

util = require('util');
console.log = function() {
    process.stdout.write((new Date()).toISOString().substring(0,19).replace('T', ' ')+': ');
    process.stdout.write(util.format.apply(this, arguments) + '\n');
};

History

  • September 15, 2013 - First version.
  • October 31, 2013 - Found a better way to do the same thing. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

R%S
United States United States
No Biography provided

You may also be interested in...

Pro

Comments and Discussions

 
Suggestionwhy dont use join ? Pin
h.petersen16-Sep-13 6:11
memberh.petersen16-Sep-13 6:11 
GeneralRe: why dont use join ? Pin
R%S16-Sep-13 8:48
memberR%S16-Sep-13 8:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180515.1 | Last Updated 31 Oct 2013
Article Copyright 2013 by R%S
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid