Click here to Skip to main content
Click here to Skip to main content

Inserting loggers in your JavaScript with JSNLog

By , 28 Nov 2012
 

Shows how to insert loggers in your JavaScript with   JSNLog, a JavaScript logging library that is well integrated with .Net. It lets you configure loggers in your web.config. And it lets you receive log messages from the client and store them on the server, without any coding.

Contents of this series

Inserting loggers in your JavaScript

Use the following methods to insert logging in your JavaScript.

getLogger

Gets a reference to a logger. If the logger does not yet exist, it will be created.

jsnlog.getLogger(loggerName);

Parameters

loggerName
Type: string

The name of the logger.

Return Value

Type: logger

A reference to the logger with the given name.

Example

var logger = jsnlog.getLogger("yForms.Validators.IsNumeric.exceptionHandler");

Recommended naming

Although you can use any naming scheme for your loggers, it tends to make sense to use this structure:

namespace.objecttype.methodname.location within method

This makes it immediately clear where in your code the logger is located. For example: 

// Create namespace
var yForms = yForms || {};

// Create Validators object constructor within namespace yForms
yForms.Validators = function() {};

// Create IsNumeric method on the Validators object
yForms.Validators.prototype.IsNumeric = function(inputValue) {
    try {
        ...
    } catch(err) {
        // Get the logger yForms.Validators.IsNumeric.exceptionHandler and log a fatal message 
        jsnlog.getLogger("yForms.Validators.IsNumeric.exceptionHandler").fatal(err.message);
    }
}

Obviously, if you do not have namespaces or objects, you could have logger names without those elements.

Appender and level inheritance

So that you do not have to configure an appender and level for every single logger, a logger inherits the level and appenders from its parent. This uses the same scheme as  Log4Net.

JSNLog works out the parent of a logger by taking away one level of its name. This means that:

  • The parent of yForms.Validators.IsNumeric.exceptionHandler is yForms.Validators.IsNumeric
  • The parent of yForms.Validators.IsNumeric is yForms.Validators
  • The parent of yForms.Validators is yForms

The parent of yForms is a special logger, called the root logger. However, apart from the fact that this root logger has no parent and no name, it  can still be used and configured as a normal logger.

getRootLogger

Returns a reference to the root logger.

jsnlog.getRootLogger();

Parameters

None

Return Value

Type: logger

A reference to the root logger.

Example

var rootLogger = jsnlog.getRootLogger();

Remarks

Normally, it makes sense to only use named loggers, because:

  • this allows you to configure each logger individually; and  
  • helps you identify the exact location in your code where a log message was generated.

However, if you decide to use only one logger, you could use the root logger. 

trace

Gets a logger to send a message with severity TRACE.

logger.trace(message);

Parameters

message
Type: object

Message to be sent. This can be any object, so doesn't have to be a string.

Return Value

None

Example

var logger = jsnlog.getLogger("yForms.Validators.IsNumeric.exceptionHandler");
logger.trace("Something interesting happened");

Or shorter:

jsnlog.getLogger("yForms.Validators.IsNumeric.interesting").trace("Something interesting happened");

You can also send an object. JSNLog will produce a message showing the values of its fields:

var myValidator = new Validator(); 
jsnlog.getLogger("yForms.Validators.IsNumeric.interesting").trace(myValidator);

debug

Similar to method trace, but sends a message of severity DEBUG.

jsnlog.getLogger("yForms.Validators.IsNumeric.interesting").debug("Something interesting happened");

info

Similar to method trace, but sends a message of severity INFO.

jsnlog.getLogger("yForms.Validators.IsNumeric.interesting").info("Something interesting happened");

warn

Similar to method trace, but sends a message of severity WARN.

jsnlog.getLogger("yForms.Validators.IsNumeric.interesting").warn("Something interesting happened");

error

Similar to method trace, but sends a message of severity ERROR.

jsnlog.getLogger("yForms.Validators.IsNumeric.interesting").error("Something interesting happened");

fatal

Similar to method trace, but sends a message of severity FATAL.

jsnlog.getLogger("yForms.Validators.IsNumeric.interesting").fatal("Something interesting happened");

time

Starts a timer.  When the timer is ended with a call to  timeEnd  using the same timer name,  the amount of time that has elapsed in milliseconds since the timer was started is logged.

logger.time(timerName, messageLevel)

Parameters

timerName
Type: string

Name assigned to the newly created timer.

messageLevel (optional)
    Type: level    

    One of:    

jsnlog.Level.TRACE
jsnlog.Level.DEBUG
jsnlog.Level.INFO (default)
jsnlog.Level.WARN
jsnlog.Level.ERROR
jsnlog.Level.FATAL    
   

    The message sent by timeEnd will have this level.     If you do not supply a level, jsnlog.Level.INFO is used.    

Return Value

None

Example

The following code uses a timer to measure how long a lengthy operation takes in milli seconds. After the operation has finished, it sends a message with severity DEBUG with the measured time.

Keep in mind that in this case, the message will only be logged if the logger has level DEBUG or higher.

jsnlog.getLogger("yForms.Validators.IsNumeric.timer1").time("timer1", jsnlog.Level.DEBUG);

... lengthy operation ...

jsnlog.getLogger('yForms.Validators.IsNumeric.timer1').timeEnd('timer1');

timeEnd

Ends a timer that was started by time and sends a message with the elapsed time.

logger.timeEnd(timerName)

Parameters

timerName
Type: string

Name of the timer as assigned by  time.

Return Value

None

Example

See the example provided for  time.

assertEqual

Compares two values using !=. If they are equal, this does nothing. Otherwise, it sends a message with severity ERROR.

logger.assertEqual(v1, v2)

Parameters

v1, v2
Type: any

If these parameters are not equal, an ERROR log message is generated.

Return Value

None

Example

var expectedValue = ....;
var actualValue = ....;

jsnlog.getLogger('yForms.Validators.IsNumeric.AssertPositive').assertEqual(expectedValue, actualValue);

Next part

In this part 2 about JSNLog, you saw in detail all JavaScript functions provided by JSNLog. The  next part, will show all elements and attributes you can use in your web.config to configure your JavaScript loggers.

If you like this article, please vote for it.

License

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

About the Author

Matt Perdeck
Web Developer
Australia Australia
Member
Matt has over 6 years .NET and SQL Server development experience. Before getting into .Net, he worked on a number of systems, ranging from the largest ATM network in The Netherlands to embedded software in advanced Wide Area Networks and the largest ticketing web site in Australia. He has lived and worked in Australia, The Netherlands, Slovakia and Thailand.
 
He recently wrote a book, ASP.NET Performance Secrets (www.packtpub.com/asp-net-site-performance-secrets/book) in which he shows in clear and practical terms how to quickly find the biggest bottlenecks holding back the performance of your web site, and how to then remove those bottlenecks. The book deals with all environments affecting a web site - the web server, the database server and the browser.
 
Matt's blog is mattperdeck.com
 
Matt currently lives in Sydney, Australia, where he works at Readify, a high profile software consultancy.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 28 Nov 2012
Article Copyright 2012 by Matt Perdeck
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid