+ All Categories
Home > Documents > Logging v.2

Logging v.2

Date post: 05-Apr-2018
Category:
Upload: alex-maskovyak
View: 215 times
Download: 0 times
Share this document with a friend

of 84

Transcript
  • 7/31/2019 Logging v.2

    1/84

    S

    Loggingor System.out.println() 2.0author: Alex Maskovyak

    date: 2012-04-27

  • 7/31/2019 Logging v.2

    2/84

    S

    Loggingor System.out.println() 2.0author: Alex Maskovyak

    date: 2012-04-27

  • 7/31/2019 Logging v.2

    3/84

    S

    Metadata

  • 7/31/2019 Logging v.2

    4/84

    Agenda

    S Objectives

    S Overview

    S Principles

    S Next Steps

  • 7/31/2019 Logging v.2

    5/84

    S

    Objectives

  • 7/31/2019 Logging v.2

    6/84

    Objectives

    S Define what logging is.

    S Understand the benefits of logging.

    S Identify logging best practices.

    S Understand how to use the new logging framework.

  • 7/31/2019 Logging v.2

    7/84

    Motivation

    S Make the life of you, the developer, easier

    S Make the life of the developer after you easier

  • 7/31/2019 Logging v.2

    8/84

    S

    Overview

  • 7/31/2019 Logging v.2

    9/84

    Definition

    S process of recording events, with an automated

    computer program, in a certain scope in order toprovide an audit trail that can be used tounderstand the activity of the system and todiagnose problems

  • 7/31/2019 Logging v.2

    10/84

    Definition

    S process of recording events, with an automated

    computer program, in a certain scope in order toprovide an audit trail that can be used tounderstand the activity of the system and todiagnose problems

  • 7/31/2019 Logging v.2

    11/84

    What it is

    S Provides insight into the state and operation of the

    system.

    S Provides application-level information in anaudience-appropriate manner.

    S Necessary operation of software during themaintenance phase of an applications life.

    S Part of the Definition of Done

  • 7/31/2019 Logging v.2

    12/84

    Purposes

    S Expose application state

    S Trace execution

    S Document configuration state

    S Monitor for errors/warnings

    S Debug code

    S Provide system performance/timing

  • 7/31/2019 Logging v.2

    13/84

    What it isnt

    S Calls to System.out.println()

    S Necessary only during development

    S Magic that occurs without developer support

    S Complete substitute for documentation, comments,and testing.

  • 7/31/2019 Logging v.2

    14/84

    Java Logging

    S Libraries

    S JDK 1.4+ java.util.logging (JUL)

    S log4j

    S nlog4j

    S log5j

    S LogBack

    S Common frameworks

    S Jakarta/Apache Commons Logging

    S Slf4j

    S Log Viewers Analyzers

    S Chainsaw

    S Lilith

  • 7/31/2019 Logging v.2

    15/84

    slf4j

    S Simple Logging Facade for Java

    S Designed by log4j creator

    S Binds to logging frameworks

    S Bridges to legacy logging APIs

    S Provides a migrator

    S Supports parameterized logging

    S slf4j-access provides servlet support

    S JBoss provides slf4j-jboss-logmanager.jar

  • 7/31/2019 Logging v.2

    16/84

    LogBack

    S Successor to log4j

    S Fast message generation

    S Automatic configuration file reloading

    S Conditional configuration files

    S Filters

    S Logback Console Plugin

  • 7/31/2019 Logging v.2

    17/84

    Java Logging ExampleJUL

  • 7/31/2019 Logging v.2

    18/84

    public class AwesomeObject {private static Logger logger =

    Logger.getLogger(AwesomeObject.class.getName());

    public static void main(String args) {logger.log(Level.INFO, Starting app, explicit Level ex.);

    logger.finer(Got arg0=+args[0]+ arg1+args[1]);

    try { crash(); }catch( Exception e ) {

    logger.severe(Encountered a problem! See exc, e);}

    logger.log(Level.FINER, Exiting application.);}

    }

  • 7/31/2019 Logging v.2

    19/84

    public class AwesomeObject {private static Logger logger =

    Logger.getLogger(AwesomeObject.class.getName());

    public static void main(String args) {logger.log(Level.INFO, Starting app, explicit Level ex.);

    logger.finer(Got arg0=+args[0]+ arg1+args[1]);

    try { crash(); }catch( Exception e ) {

    logger.severe(Encountered a problem! See exc, e);}

    logger.log(Level.FINER, Exiting application.);}

    }

  • 7/31/2019 Logging v.2

    20/84

    public class AwesomeObject {private static Logger logger =

    Logger.getLogger(AwesomeObject.class.getName());

    public static void main(String args) {logger.log(Level.INFO, Starting app, explicit Level ex.);

    logger.finer(Got arg0=+args[0]+ arg1+args[1]);

    try { crash(); }catch( Exception e ) {

    logger.severe(Encountered a problem! See exc, e);}

    logger.log(Level.FINER, Exiting application.);}

    }

  • 7/31/2019 Logging v.2

    21/84

    public class AwesomeObject {private static Logger logger =

    Logger.getLogger(AwesomeObject.class.getName());

    public static void main(String args) {logger.log(Level.INFO, Starting app, explicit Level ex.);

    logger.finer(Got arg0=+args[0]+ arg1+args[1]);

    try { crash(); }catch( Exception e ) {

    logger.severe(Encountered a problem! See exc, e);}

    logger.log(Level.FINER, Exiting application.);}

    }

  • 7/31/2019 Logging v.2

    22/84

    public class AwesomeObject {private static Logger logger =

    Logger.getLogger(AwesomeObject.class.getName());

    public static void main(String args) {logger.log(Level.INFO, Starting app, explicit Level ex.);

    logger.finer(Got arg0=+args[0]+ arg1+args[1]);

    try { crash(); }catch( Exception e ) {

    logger.severe(Encountered a problem! See exc, e);}

    logger.log(Level.FINER, Exiting application.);}

    }

  • 7/31/2019 Logging v.2

    23/84

    public class AwesomeObject {private static Logger logger =

    Logger.getLogger(AwesomeObject.class.getName());

    public static void main(String args) {logger.log(Level.INFO, Starting app, explicit Level ex.);

    logger.finer(Got arg0=+args[0]+ arg1+args[1]);

    try { crash(); }catch( Exception e ) {

    logger.severe(Encountered a problem! See exc, e);}

    logger.log(Level.FINER, Exiting application.);}

    }

  • 7/31/2019 Logging v.2

    24/84

    public static void main(String args) {logger.entering(AwesomeObject.class.getName(),main,args);

    try { crash(); }

    catch( Exception e ) {logger.logp(Level.SEVERE,AwesomeObject.class.getName(),main,Encountered a problem! See exc, e);

    }

    logger.exiting(AwesomeObject.class.getName(),main,Exiting application.,0);

    }

  • 7/31/2019 Logging v.2

    25/84

    public static void main(String args) {logger.entering(AwesomeObject.class.getName(),main,args);

    try { crash(); }

    catch( Exception e ) {logger.logp(Level.SEVERE,AwesomeObject.class.getName(),main,Encountered a problem! See exc, e);

    }

    logger.exiting(AwesomeObject.class.getName(),main,Exiting application.,0);

    }

  • 7/31/2019 Logging v.2

    26/84

    public static void main(String args) {logger.entering(AwesomeObject.class.getName(),main,args);

    try { crash(); }

    catch( Exception e ) {logger.logp(Level.SEVERE,AwesomeObject.class.getName(),main,Encountered a problem! See exc, e);

    }

    logger.exiting(AwesomeObject.class.getName(),main,Exiting application.,0);

    }

  • 7/31/2019 Logging v.2

    27/84

    Java Logging Exampleslf4j & LogBack

  • 7/31/2019 Logging v.2

    28/84

    public class AwesomeObject {private static Logger logger =

    Logger.getLogger(AwesomeObject.class.getName());

    public static void main(String args) {logger.info(Starting application.);

    logger.finer(Got arg0=+args[0]+ arg1+args[1]);

    try { crash(); }catch( Exception e ) {

    logger.severe(Encountered a problem! See exc, e);}

    logger.log(Level.FINER, Exiting application.);}

    }

  • 7/31/2019 Logging v.2

    29/84

    public class AwesomeObject {private static Logger logger =

    LoggerFactory.getLogger(AwesomeObject.class.getName());

    public static void main(String args) {logger.info(Starting application.);

    logger.finer(Got arg0=+args[0]+ arg1+args[1]);

    try { crash(); }catch( Exception e ) {

    logger.severe(Encountered a problem! See exc, e);}

    logger.log(Level.FINER, Exiting application.);}

    }

  • 7/31/2019 Logging v.2

    30/84

    public class AwesomeObject {private static Logger logger =

    LoggerFactory.getLogger(AwesomeObject.class.getName());

    public static void main(String args) {logger.info(Starting application.);

    logger.finer(Got arg0=+args[0]+ arg1+args[1]);

    try { crash(); }catch( Exception e ) {

    logger.severe(Encountered a problem! See exc, e);}

    logger.log(Level.FINER, Exiting application.);}

    }

  • 7/31/2019 Logging v.2

    31/84

    public class AwesomeObject {private static Logger logger =

    LoggerFactory.getLogger(AwesomeObject.class.getName());

    public static void main(String args) {logger.info(Starting application.);

    logger.debug(Got args: {}, args);

    try { crash(); }catch( Exception e ) {

    logger.severe(Encountered a problem! See exc, e);}

    logger.log(Level.FINER, Exiting application.);}

    }

  • 7/31/2019 Logging v.2

    32/84

    public class AwesomeObject {private static Logger logger =

    LoggerFactory.getLogger(AwesomeObject.class.getName());

    public static void main(String args) {logger.info(Starting application.);

    logger.debug(Got args: {}, args);

    try { crash(); }catch( Exception e ) {

    logger.severe(Encountered a problem! See exc, e);}

    logger.log(Level.FINER, Exiting application.);}

    }

  • 7/31/2019 Logging v.2

    33/84

    public class AwesomeObject {private static Logger logger =

    LoggerFactory.getLogger(AwesomeObject.class.getName());

    public static void main(String args) {logger.info(Starting application.);

    logger.debug(Got args: {}, args);

    try { crash(); }catch( Exception e ) {

    logger.error(Encountered a problem! See exc, e);}

    logger.log(Level.FINER, Exiting application.);}

    }

  • 7/31/2019 Logging v.2

    34/84

    public class AwesomeObject {private static Logger logger =

    LoggerFactory.getLogger(AwesomeObject.class.getName());

    public static void main(String args) {logger.info(Starting application.);

    logger.debug(Got args: {}, args);

    try { crash(); }catch( Exception e ) {

    logger.error(Encountered a problem! See exc, e);}

    logger.log(Level.FINER, Exiting application.);}

    }

  • 7/31/2019 Logging v.2

    35/84

    public class AwesomeObject {private static Logger logger =

    LoggerFactory.getLogger(AwesomeObject.class.getName());

    public static void main(String args) {logger.info(Starting application.);

    logger.debug(Got args: {}, args);

    try { crash(); }catch( Exception e ) {

    logger.error(Encountered a problem! See exc, e);}

    logger.log(new myExitMarker(), Exiting application.);}

    }

  • 7/31/2019 Logging v.2

    36/84

    Know Your Audience

    S System administrators/operators

    S Installing application.

    S Starting application.

    S Shutting down application.

  • 7/31/2019 Logging v.2

    37/84

    Know Your Audience

    S Support Personnel

    S Monitors of application.

  • 7/31/2019 Logging v.2

    38/84

    Know Your Audience

    S Developers

    S Debugging applications.

    S Measuring performance.

    S Using your class/API.

  • 7/31/2019 Logging v.2

    39/84

    Know Your Audience

    S Machines

    S Scripts

    S Automation tools

    S Operation tools

  • 7/31/2019 Logging v.2

    40/84

    Severity Levels

    S Partition log space

    S Digestible pieces

    S Triage-able levels.

  • 7/31/2019 Logging v.2

    41/84

    Not Triage-able

    S DEBUG: Began session

    S DEBUG: DB access pass for user_id=154314, measure_id=14.

    S DEBUG: DB access fail for user_id=41234113, measure_id=15.

    S DEBUG: DB access pass for user_id=1341234, measure_id=14.

    S DEBUG: DB access pass for user_id=8224122, measure_id=14.

    S DEBUG: DB access pass for user_id=1143456, measure_id=14.

    S DEBUG: DB access pass for user_id=17111341, measure_id=14.

    S DEBUG: DB access fail for user_id=17521344113, measure_id=14.

    S DEBUG: DB access pass for user_id=17, measure_id=131422.

    S DEBUG: Ended session

  • 7/31/2019 Logging v.2

    42/84

    via audit tool...or even a grep

    S DEBUG: Began session

    S DEBUG: DB access pass for user_id=154314, measure_id=14.

    S ERROR: DB access fail for user_id=41234113, measure_id=15.

    S DEBUG: DB access pass for user_id=1341234, measure_id=14.

    S DEBUG: DB access pass for user_id=8224122, measure_id=14.

    S DEBUG: DB access pass for user_id=1143456, measure_id=14.

    S DEBUG: DB access pass for user_id=17111341, measure_id=14.

    S ERROR: DB access fail for user_id=17521344113, measure_id=14.

    S DEBUG: DB access pass for user_id=17, measure_id=131422.

    S DEBUG: Ended session

  • 7/31/2019 Logging v.2

    43/84

    Severity Levels

    S FATAL

    S Application down.

    S Application inaccessible.

    S High-level features are inaccessible.

    S Application aborted.

  • 7/31/2019 Logging v.2

    44/84

    Severity Levels

    S ERROR (SEVERE)

    S Application was unable to recover from a problem.

    S Application was able to recover but was unable tofulfill a request or contract.

  • 7/31/2019 Logging v.2

    45/84

    Severity Levels

    S WARN

    S Potentially harmful problem has occurred that warrantsfurther investigation.

    S No fault occurred.

  • 7/31/2019 Logging v.2

    46/84

    Severity Levels

    S INFO

    S Broad-brush

    S High-level

    S Coarse-grain

    S System progress or state.

  • 7/31/2019 Logging v.2

    47/84

    Severity Levels

    S DEBUG (FINE)

    S Low-level

    S Fine-grain

    S Information for debugging

    S Generally turned off on production systems unless a

    problem is detected where high-level information isinsufficient.

  • 7/31/2019 Logging v.2

    48/84

    Hierarchies

    S Further separation of concerns

  • 7/31/2019 Logging v.2

    49/84

    Hierarchies

    S NONE: All technical details

    S TIMING: Performance info

  • 7/31/2019 Logging v.2

    50/84

    Hierarchies

    S SECURITY: Security relevant information

    S Failed logins

    S Creation of users

    S Access to invalid URLs

    S Potentially unsafe user-input

    S Spikey user requests

  • 7/31/2019 Logging v.2

    51/84

    Hierarchies

    S AUDIT

    S Completed / Failed business transactions.

    S DB access.

    S Service access.

  • 7/31/2019 Logging v.2

    52/84

    Hierarchies

    S USER: user behavior

    S Invalid data entered

    S Actions used

  • 7/31/2019 Logging v.2

    53/84

    Persistence

    S Multiple files.

    S Per application.

    S Per date.

    S Per hierarchy/concerns.

  • 7/31/2019 Logging v.2

    54/84

    Persistence

    S File-system first, HBase later.

    S /usr/local/explorys/[application-name]/

    S [application-name]-[date].log

  • 7/31/2019 Logging v.2

    55/84

    S

    PrinciplesWhat, Where, When, & How

  • 7/31/2019 Logging v.2

    56/84

    Principle #1

    System.out is not adequate logging

  • 7/31/2019 Logging v.2

    57/84

    Principle #2

    Logging should occur during development

  • 7/31/2019 Logging v.2

    58/84

    Principle #3

    Logging must be descriptive and useful to others

    Principle #3

  • 7/31/2019 Logging v.2

    59/84

    Principle #3Ex.1

    S NO (does not contain valuable information):

    S log.debug(Here);

    S log.debug(+++DEBUG+++);

    S log.debug(randomVariableWithoutLogComment);

    Principle #3

  • 7/31/2019 Logging v.2

    60/84

    Principle #3Ex.2

    S NO:

    S log.debug(Array values: {}, myArray);S Will print out a reference identity: Array@4dc911a5

    S Does not have an overloaded toString();

    S YESS log.debug(Array values: {}, Arrays.toString(myArray));

    S Prints out a human-readable string value.

    Principle #3

  • 7/31/2019 Logging v.2

    61/84

    Principle #3Recipe for a Good Message

    S What is your reason for logging

    / Why are you logging.S Root cause.

    Principle #3

  • 7/31/2019 Logging v.2

    62/84

    Principle #3Recipe for a Good Message

    S What needed to happen

    / Affected use-case.S User creation?

    S Measure execution?

    S Measure deletion?

    Principle #3

  • 7/31/2019 Logging v.2

    63/84

    Principle #3Recipe for a Good Message

    S What instance was effected?

    S User id.

    S Measure id.

    S Measure group id.

    Principle #3

  • 7/31/2019 Logging v.2

    64/84

    Principle #3Recipe for a Good Message

    S What did and what didnt happen

    / Effect on Use-caseS User needs to repeat action, or did it succeed?

    S Did entire batch fail, or did just a single step?

    Principle #3

  • 7/31/2019 Logging v.2

    65/84

    Principle #3Where & When

    S Application state changes

    S Startup

    S Shut-down

    Principle #3

  • 7/31/2019 Logging v.2

    66/84

    Principle #3Where & When

    S Configuration

    S Source of configurationS Files used

    S Environment

    S VM arguments

    S

    Values of parametersS Packages/Libraries

    Principle #3

  • 7/31/2019 Logging v.2

    67/84

    Principle #3Where & When

    S Method Invocation

    S StartS Method name

    S Parameter names/values

    S Time entered

    S End

    S Method return valueS Exit statement

    S Time returned

    Principle #3

  • 7/31/2019 Logging v.2

    68/84

    Principle #3Ex.3

    public Measures[] getMeasures(User user, String type) {log.debug(

    BEGIN getMeasures(user={}, type={}),user, type);

    Measures[] res = ;log.debug(END getMeasures() => {}, res.length);return res;

    }

    Principle #3

  • 7/31/2019 Logging v.2

    69/84

    Principle #3Where & When

    S Exceptions

    S Where the exception is actually handled/caught.S Top-most level.

    S Not where it is transformed.

    S Not when it is first thrown.

    S

    When all information is availableS Avoid repetition

    Principle #3

  • 7/31/2019 Logging v.2

    70/84

    Principle #3Ex.4

    S NO:try {}catch(Exception e) {

    log.error(Caught exception, did nothing, e);throw e;

    }

    S NO:

    Exception e = ;log.error(Created exc and printed it redundantly here, e);

    throw e;

    Principle #3

  • 7/31/2019 Logging v.2

    71/84

    Principle #3Where & When

    S Long-lived Calls

    S e.g.S IO

    S File

    S DB

    S Services

    S SOAS REST

    S Log before & after call

  • 7/31/2019 Logging v.2

    72/84

    Principle #4

    Logging should be DRY and concise.

  • 7/31/2019 Logging v.2

    73/84

    Principle #5

    Log with an audience in mind.

    Principle #5

  • 7/31/2019 Logging v.2

    74/84

    Principle #5Ex.1

    S ADMIN:

    S log.error(PDV failed to start because the datasource {} couldnot be accessed, please ensure that {} is accessibleby);

    S DEV:

    S log.error(PDV attempted to do {} but was unable to becauseof exception {});

  • 7/31/2019 Logging v.2

    75/84

    Principle #6

    Be mindful of performance

    Principle #6

  • 7/31/2019 Logging v.2

    76/84

    Principle #6Ex.1

    S Acquire a single logger instance.

    S private Logger logger =LoggerFactory.getLogger(classname);

    S @Inject Logger logger;

    S Avoid processing large collections of objects.

    S No: log.info(Returning measures {}, measures);

    S Yes: log.info(Returning measured ids {}, );

    S Yes: log.info(Returning total measure id count {}, );

    Principle #6

  • 7/31/2019 Logging v.2

    77/84

    Principle #6Ex.2

    S Logging info quantity/size can/should be altered

    based upon status.

    If (log.isDebugEnabled()) {log.debug(Measures requested {});

    } else {

    log.info(Measure ids requested {});}

  • 7/31/2019 Logging v.2

    78/84

    Principle #7

    Use performance values appropriately.

    Principle #7 Ex 1

  • 7/31/2019 Logging v.2

    79/84

    S Yes:

    StopWatch watch = new LoggingStopWatch( dbio );longlivedIO();watch.stop();

    S Yes:

    log.info(Finished {} measure results in {} secs.);log.info(Average calculation {} secs / result);

    S No:

    public String getName() {}

    Principle #7 Ex.1

  • 7/31/2019 Logging v.2

    80/84

    Principle #8

    S Be safe

    S Avoid causing problems

    S Avoid side effects

    Principle #8

  • 7/31/2019 Logging v.2

    81/84

    Principle #8Ex.1

    S log.debug(Obtained {}, user.getId());S are you sure user isnt null?

    S log.debug(Obtained {}, collection.get(1));S are you sure there there are enough elements?

    S log.debug(Connection closed: {}, conn.close());S are you sure debug level statements are enabled?

    S are you sure theyll be enabled in production?

  • 7/31/2019 Logging v.2

    82/84

    S

    Next Steps

  • 7/31/2019 Logging v.2

    83/84

    Action Items

    S Alex

    S Test slf4j and LogBack integration in current environment.S Test slf4j migrator.

    S Test LogBack Eclipse tool.

    S Team

    S Study logger use.

    S Begin using standard jdk logging in the areas identified.

    S Be mindful of best-practices and principles.

  • 7/31/2019 Logging v.2

    84/84


Recommended