30
©2010 Improving Enterprises, Inc. Logging For Fun and Profit Jane Prusakova Jane.Prusakova@ImprovingEnterprise s.com http:// softwareandotherthings.blogspot.co m Improving Enterprises AA.com 2013

Application logging for fun and profit

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Logging For Fun and Profit

Jane Prusakova

[email protected]

http://softwareandotherthings.blogspot.com

Improving Enterprises

AA.com

2013

Page 2: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Why

How-to

Using the results

Page 3: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Why logging?

Enquiring minds want to know!

Avoid programming-by-coincidence  

Trace what the users do

See effects of Integration

Multithreading

High load

Page 4: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

When to use logging?Greenfield development

Brownfield developmentwhat came before

the effect of changes

Maintenance and troubleshootingtrace bugs

BonusEvolve apps based on users behavior

Page 5: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Logging [vs Debugging]

Set up once, always ready to use

Minimum disruption to the flow

Works locally and in the cloud

Faster data collection

Collected data is persisted

Page 6: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

How-to

Using the results

Page 7: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Yes, I want to setup logging!

Pick a framework

Proper configuration

Consider ways to parse logs

Logging will change with the application

Page 8: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Log4J SLF4J

Apache MIT license

Logging framework Logging facade

Configuration, log levels and categories, rotate log files, thread-safe logging.

Support: online documentation, tutorials, online forums.

Page 9: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Where to write logs?

Console

File system

DB

Queue mechanisms

Combination

Page 10: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Database Files

Easy to parse, hard to evolve

Very hard to parse, easy to change

Uses DB connections

Requires FS access

Can cause app crash, loss of logs,

slow down app

Can fill up space

Page 11: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Configuring Log4J

Java properties-style

XML

Programmatically

Hard-coded

Takes preference

Page 12: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Log Message

What to log with the message

Importance level

When

Where in the code

Data being processed

Thread information

Page 13: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Example layout

log4j.appender.stdout.layout.ConversionPattern= %d %5p [%t] (%F:%L) - %m%n

- Date- Log Level - Thread name- File and line (slow to retrieve)

Page 14: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Enough and but not too much

Logging should support in code

correctness

readability

performance

Consider making logs easier to use

Page 15: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Best Practices

Relate each message to generating point

Code

Data

Format messages for readability

Use .ToString on objects

Page 16: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

More Best Practices

Input and output Errors and exceptionsTransitionsIntegration pointsThread rendezvous

Page 17: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Log input and output

public int calculateCost(String user, Flight flight) {

log.debug(“calculateCost: user “ + user + “: “ + flight);

int cost = … ;

… // cost calculation

log.debug((“calculateCost: cost for “ + user + “ on “ + flight + “: “ + cost);

return cost; }

Page 18: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Log errors and exceptions

// do dangerous workresult = DoDangerousWork(user, flight);

}

catch (Exception e){

log.error(“methodName: exception in DoDangerousWork for user “ + user + “ on “ + flight, e);

// recover or re-throw }

Page 19: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

More

Log destination, parameters, outcomesDB calls

SOAP calls

Wait timeThread joins

Available memoryMemory-intensive operations

Intermediate calculation resultsFor complicated calculations

Page 20: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Practices to avoid

Little information… // step 1

Logger.info(“did something”);

… // ste 2

Logger.info(“did more work”);

foreach (…) {

… // useful work

Logger.info(“working hard”);

}

Page 21: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Watch for logging-caused bugs

Side effects

Logger.info(count++ “: did something”);

Errors and exceptionsLogger.info(m.GetValue());

Page 22: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

More practices to avoid

DRY principle applies

Page 23: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Using the results

Page 24: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

You’ve got logs!

Rotate logs: by size, by time

Retention policy

2-level storage

Provide access - read only!

Transfer and analysis

Warning: large datasets

Page 25: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Security and performance

Moving and processing

can impact performance

Absolutely no sensitive

data

Page 26: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Learning from the logs

Never rely on eye-balling

WYS is not WYG

Aggregate data

Distinguish common and

rare

Page 27: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Tools

Process datasets:

Line by line grep, Perl, awk, cut, tail, head

Load entire file to eyeballvi, notepad++

Better way to eyeballmore, less

Page 28: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Learning from the logs

> grep userName MyApplication.log

grep methodName MyApplication.log

grep threadName MyApplication.log

tail -2000 VeryImportant.log | grep Exception

grep FATAL MyApplication.log

Page 29: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Querying logs using AwkGNU Awk www.gnu.org/software/gawk

create histogramsWait times

Number of calls

Exceptions per time period or per user

Compare and relate events Exception stacks traces

Outcomes by caller or thread

Page 30: Application logging for fun and profit

©2010 Improving Enterprises, Inc.

Logging For Fun and Profit

Jane Prusakova

[email protected]

http://softwareandotherthings.blogspot.com

Improving Enterprises

AA.com

2013