JavaScript – Log to Console (console.log vs. frameworks)


developers want to recognize what their code is doing while it’s operating JavaScript, whether in the browser or the NodeJS atmosphere. Such data can lend a hand check code, find bugs, or measure efficiency. Printing data by means of the console is probably the most basic approach to try this. in this article, we’ll discuss logging to the browser console the use of the console.log() approach and logging using standard frameworks and libraries. common: Logging to the Browser Console the usage of console.log() On the shopper facet or in the browser, you should utilize console.log() to log any data within JavaScript. the use of console.log() in Javascript or the Browser Hit the F12 key to open the browser console. be sure the “Console” tab is chosen, then that you could replica and paste these examples right away into the console command line: 1 2 three four 5 6 console.log( ‘foo’ ); // string console.log( ‘foo price: ‘, one hundred ); // string with some worth console.log( ‘foo’, ‘bar’, ‘baz’ ); // a couple of strings console.log( identify: “Simon”, id: 5 ); // object console.log( [ 1, 2, 3 ], [ ‘a’, ‘b’, ‘c’ ] ); // multiple arrays console.log( new Date() ); // date An example the use of String Substitution that you may additionally use string substitution, which is mainly helpful with code loops. 1 2 3 4 5 6 7 8 var contacts = [ name: ‘Simon’, id: 5, age: 25 , name: ‘Ben’, id: 7, age: 54 , name: ‘Lisa’, id: 9, age: 32 ]; for (var i=0; i to stdout and a couple of> to stderr. An instance of methods to Redirect Output in NodeJS 1 2 console.log(‘this is simply noisy debug stuff’); console.error(‘this means any person broke something again’); we are able to redirect both output (stdout) and error (stderr) to completely different recordsdata: 1 node take a look at.js > out.log 2> err.log we could also redirect each output and mistakes to the identical file: 1 node test.js > everything.log 2>&1 developed: the use of Frameworks & Libraries to Log to the Console For a more evolved logging resolution in the browser, log4javascript can also be helpful. For extra critical NodeJS logging functions, it may be just right to make use of a well-examined logging library, similar to Winston, Node-Bunyan, or Tracer. Logging to the Browser Console using JS Framework: log4javascript this can be a JS logging framework with an API based on to the popular Java logging library log4j. main features of log4javascript: by means of default, it logs to a pop-up console window with highly effective search (together with regular expression) and filtering options. The window may also be used as an inline iframe on the web page. Log messages may also be sent to the server by way of HTTP (or AJAX, in case you like). It’s highly configurable using familiar strategies from log4j, together with PatternLayout, which supplies the developer complete regulate over the structure of the log messages. here’s a lightweight version of log4javascript (helps a core subset of the original options). Logging to NodeJS Console using a Library: Winston, Node-Bunyan, & Tracer relying on the process context, the asynchronous nature of logging can result in misplaced log messages if no longer taken care of properly. That’s where logging libraries for NodeJS transform moderately helpful. 1. Winston Library the most popular NodeJS logging library seems to be Winston, which is designed to be simple and common. Winston helps asynchronous transports (or storage devices to your logs), so each and every occasion can have multiple transports configured at different levels. for instance, that you would be able to have error logs consistently stored in a faraway location (like a database), but all logs output to the console or a neighborhood file. 2. Node-Bunyan Library The Node-Bunyan library gives a easy and quick JSON logging module for NodeJS. the theory of this library is that server logs should be structured and that JSON is an efficient format for that. A log report is one line of JSON.stringify() output. there’s also a different command line instrument for the printing and filtering of Bunyan logs. 3. Tracer Library Tracer is a customizable NodeJS logging library that may print log messages with a timestamp, file identify, manner identify, line number, direction, or call stack. Tracer also has enhance for consumer-defined logging ranges, customized output templates, different transports, and colored output.


Leave A Reply