Logging in Java, C# and Python

In Java & C#, the most commonly used logging utilities are log4j and log4net. Python has it’s own inbuilt logging module which includes the exact same features as other log4X brethren!

Setting up logging module in python >>

import logging

def initLogger(appName='Application', handlerType='StreamHandler', \
    loggerLevel='INFO', handlerLevel='DEBUG'):
    '''
        * There are many handler types available such as >>
            StreamHandler
            FileHandler
            RotatingFileHandler
            TimedRotatingFileHandler
            SocketHandler
            DatagramHandler
            SysLogHandler
            NTEventLogHandler
            SMTPHandler
            MemoryHandler
            HTTPHandler

            You may have to customize this func to use other handlers.

        * Many levels available such as >>
            debug
            info
            warning
            error
            critical
    '''
    # Creating the logger object
    log = logging.getLogger(appName)
    log.setLevel(getattr(logging, loggerLevel))

    # Initializing logging settings in handler
    handler = getattr(logging, handlerType)()
    handler.setLevel(getattr(logging, handlerLevel))
    handler.setFormatter(logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - %(message)s'))

    # binding the handler to Logger object
    log.addHandler(handler)

    return log

def main():
    logger = initLogger('Test App')
    logger.info('Hello World!')

    raw_input()

if __name__ == '__main__':
    main()

More information found at >> http://onlamp.com/pub/a/python/2005/06/02/logging.html

Setting up logger in C# >>

You’ll need to create a configuration file (similar to the one below) and then define a logger instance in Code mapped to the configuration.

More information can be found >> http://logging.apache.org/log4net/release/manual/configuration.html

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="log4net"
       type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>

    <appender name="LogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="c:\LogTest2.txt" />

      <param name="AppendToFile" value="true" />
      <param name="rollingStyle" value="Size" />

      <param name="maxSizeRollBackups" value="10" />
      <param name="maximumFileSize" value="10MB" />
      <param name="staticLogFileName" value="true" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
      </layout>
    </appender>

    <appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
      <layout type="log4net.Layout.PatternLayout">
        <param name="Header" value="[Header]\r\n" />
        <param name="Footer" value="[Footer]\r\n" />
        <param name="ConversionPattern" value="%d [%t] %-5p %c %m%n" />
      </layout>
    </appender>

    <root>
      <level value="ALL" />
      <appender-ref ref="LogFileAppender" />
      <appender-ref ref="ConsoleAppender" />
    </root>
  </log4net>
</configuration>

leave your comment