13-Jul-15 Logging. What is logging? “Logging” is producing messages that tell you what your program is doing It’s not much different than using System.out.println(...)

Slides:



Advertisements
Similar presentations
11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
Advertisements

Mr. Wortzman.  So far, we have gotten all our input and written all our output to the console  In reality, this is somewhat uncommon  Instead, we often.
Logging in Java applications Sean C. Sullivan July 23, 2002 Portland Java Users Group.
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Error Handling with Exceptions Concepts C and other earlier languages often had multiple error-handling schemes, and these were generally established.
Debugging & Logging. Java Logging Java has built-in support for logging Logs contain messages that provide information to – Software developers (e.g.,
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
23-Jun-15 Logging. What is logging? “Logging” is producing messages that tell you what your program is doing It’s not much different than using System.out.println(...)
26-Jun-15 Logging. What is logging? “Logging” is producing messages that tell you what your program is doing It’s not much different than using System.out.println(...)
CONTENTS:-  What is Event Log Service ?  Types of event logs and their purpose.  How and when the Event Log is useful?  What is Event Viewer?  Briefing.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
Week 14 - Monday.  What did we talk about last time?  Image manipulation  Inheritance.
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
Java Software Solutions Foundations of Program Design Sixth Edition
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Coding Methodology How to Design Code. © 2005 MIT-Africa Internet Technology Initiative Pay Attention to Detail When implementing or using APIs details.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Introduction to Testing 1. Testing  testing code is a vital part of the development process  the goal of testing is to find defects in your code  Program.
1 Software Construction and Evolution - CSSE 375 Exception Handling – Logging & Special Situations Steve Chenoweth Office: Moench Room F220 Phone: (812)
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
1 Software Construction and Evolution - CSSE 375 Exception Handling - Principles Steve Chenoweth, RHIT Above – Exception handling on the ENIAC. From
CIT 590 Intro to Programming First lecture on Java.
Introduction to Exception Handling and Defensive Programming.
Logging1. 2 Introduction Ships must keep a written log telling speed, direction, destination, etc. –A kind of diary of the ship. Large programs should.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Vinay Paul. CONTENTS:- What is Event Log Service ? Types of event logs and their purpose. How and when the Event Log is useful? What is Event Viewer?
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Creating a GUI Class An example of class design using inheritance and interfaces.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
BIT 115: Introduction To Programming Professor: Dr. Baba Kofi Weusijana (say Doc-tor Way-oo-see-jah-nah, Doc-tor, or Bah-bah)
Internet Flow By: Terry Hernandez. Getting from the customers computer onto the internet Internet Browser
Simple Java I/O Part I General Principles. Streams All modern I/O is stream-based A stream is a connection to a source of data or to a destination for.
Exception Handling. VB.NET has an inbuilt class that deals with errors. The Class is called Exception. When an exception error is found, an Exception.
Introduction to Exceptions in Java CS201, SW Development Methods.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Lecture 1 Page 1 CS 111 Summer 2013 Important OS Properties For real operating systems built and used by real people Differs depending on who you are talking.
Exceptions Part II. What you need to know Last time –What happens when an exception is thrown –What are your choices for handling exceptions –The different.
CVS, Logging, Development
Summary prepared by Kirk Scott
Logger, Assert and Invariants
Some Eclipse shortcuts
Creating and Modifying Text part 2
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Fundamental Error Handling
Debugging & Logging.
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions 2nd Lecture.
Exceptions 19-Feb-19.
CMSC 202 Exceptions 2nd Lecture.
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
CSC 143 Java Errors and Exceptions.
Exceptions 10-May-19.
Exceptions 5-Jul-19.
CMSC 202 Exceptions.
CS 240 – Advanced Programming Concepts
Presentation transcript:

13-Jul-15 Logging

What is logging? “Logging” is producing messages that tell you what your program is doing It’s not much different than using System.out.println(...) Log messages can go to the console, to a file, or one of several other places (e.g. sent over the Internet) You can use logging to help you debug a program You can use logging to produce a file when the user runs your program

Why use logging? Reality: Large programs always have bugs JUnit testing can greatly reduce this problem, but it’s impossible to see in advance all potentially useful tests Your program may go to customers (users) Typical error report from customer: “It doesn’t work.” We love it when we have a customer who tries assorted things and gives us a detailed scenario in which the program fails Such customers are rare Here’s what you want to tell the typical customer: “Send us the log; it’s in such-and-such a place.”

Basic use import java.util.logging.*; private static Logger myLogger = Logger.getLogger("myPackage"); myLogger.log(Level.SEVERE, "Bad news!"); Aug 15, :51:09 AM myPackage.Sync main SEVERE: Bad news!

Logging levels and methods Level.SEVERE Level.WARNING Level.INFO Level.CONFIG Level.FINE Level.FINER Level.FINEST These levels are ordered, so that you can set the level of severity that results in log messages However, the levels have no inherent meaning--they are what you make of them myLogger.severe(String msg ); myLogger.warning(String msg ); myLogger.info(String msg ); myLogger.config(String msg ); myLogger.fine(String msg ); myLogger.finer(String msg ); myLogger.finest(String msg );

Controlling logging levels public void Logger.setLevel(Level newLevel ) Sets the logger to log all messages at newLevel or above Logger calls at lower levels don’t do anything Example: logger.setLevel(Level.WARNING); Additional settings: logger.setLevel(Level.ALL); logger.setLevel(Level.OFF); public Level getLevel() Note that this returns a Level, not an int Level has intValue() and toString() methods

Additional Logger methods void entering(String sourceClass, String sourceMethod ) void entering(String sourceClass, String sourceMethod, Object param1 ) void entering(String sourceClass, String sourceMethod, Object[] params ) void exiting(String sourceClass, String sourceMethod ) void exiting(String sourceClass, String sourceMethod, Object result ) These log messages at level FINER

Logging flow of control You send your message to a Logger The Logger checks a Filter to see whether to ignore the message The Logger sends the message to a Handler to put the message somewhere The Handler checks another Filter to see whether to ignore this kind of message sent to this destination The Handler calls a Formatter to decide what kind of a text string to produce The Handler sends the formatted message somewhere Logger Handler Filter Formatter applicationdestination

Filters Filter is an interface; it defines the single method boolean isLoggable(LogRecord record ) A LogRecord is another class in java.util.logging ; it provides numerous methods for examining the proposed logging message We won’t go into the details of LogRecord in this lecture (see the API if you need to use this)

Logging formatting and destinations The JDK defines five Handlers: StreamHandler : sends messages to an OutputStream ConsoleHandler : sends messages to System.err (default) FileHandler : sends messages to a file SocketHandler : sends messages to a TCP port MemoryHandler : buffers messages in memory It also defines two ways to format messages: SimpleFormatter (default) XMLFormatter And, of course, you can define your own Handler s and Formatter s As you can tell from the “Basic Use” slide earlier, you can ignore all of this and just use the defaults

Using a FileHandler try { logger.addHandler(new FileHandler("myLogFile")); } catch (SecurityException e) { e.printStackTrace(); } The default Formatter for a FileHandler is XMLFormatter

FileHandler results T13:21: myPackage SEVERE myClass main 10 Bad news!

Reading log files Don’t like XML? See the SimpleFormatter class When you log, your log messages must be exceptionally easy to understand Remember, you might see a log file months or even years after you wrote the program Messages such as at1: may no longer make any sense to you

What to log Here’s what you don’t want to log: Everything! Specific methods only after an error has occurred Here’s what you do want to log: “Critical events,” such as updating a database Results of a long sequence of operations Interactions with other components of a large system Actions known to be complex or error-prone

When logging goes bad Here’s an all-too-common error: Create log files on every run of the program Never get rid of old log files Solution: Use the FileHandler class The FileHandler can either write to a specified file, or it can write to a rotating set of files. For a rotating set of files, as each file reaches a given size limit, it is closed, rotated out, and a new file opened. Successively older files are named by adding "0", "1", "2", etc into the base filename.

The rest of the story As usual, I’ve only given you a few features beyond the most basic usage If you want to know about Filter s, other destinations than the console or a file, etc., it’s all in the Java API You should also know that there is an alternative open source logging package, log4j log4j has been around longer than Sun’s logging package Many people feel that log4j is better I didn’t cover it because it isn’t installed in the labs here

The End