CS 240 – Advanced Programming Concepts

Slides:



Advertisements
Similar presentations
Logging in Java applications Sean C. Sullivan July 23, 2002 Portland Java Users Group.
Advertisements

„Exceptions”. Exceptions Handling errors with exceptions Golden rule of programming: Errors occur in software programs. What really matters is what happens.
CHAPTER 12 GENERICS Introduction to java 1. Assignment 5 Solution See Eclipse.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Debugging & Logging. Java Logging Java has built-in support for logging Logs contain messages that provide information to – Software developers (e.g.,
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(...)
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(...)
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
ACS Logging System Concepts and Example H.Sommer (Restructured, based on slides from previous years) UTFSM Valparaiso, Chile, Nov ACS Logging System.
Java Software Solutions Foundations of Program Design Sixth Edition
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
Logging with Log4j. Introduction Logging - chronological and systematic record of data processing events in a program. Possible goals: Create an audit.
Simple Web Services. Internet Basics The Internet is based on a communication protocol named TCP (Transmission Control Protocol) TCP allows programs running.
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
Exception Handling. Outline What is an Exception How to use exceptions catch ing throw ing Extending the Exception class Declaring using the throws clause.
LogBox Enterprise Logging Brad Wood
1 Software Construction and Evolution - CSSE 375 Exception Handling – Logging & Special Situations Steve Chenoweth Office: Moench Room F220 Phone: (812)
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
Sonic EventMonitor Monitoring your Sonic environment Tako Grijpma Progaia Resource Solutions 09 november 2006.
Logging1. 2 Introduction Ships must keep a written log telling speed, direction, destination, etc. –A kind of diary of the ship. Large programs should.
Dynamic Architectures (Component Reconfiguration) with Fractal.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Jaas Introduction. Outline l General overview of Java security Java 2 security model How is security maintained by Java and JVM? How can a programmer.
The Log4E logging plug-in David Gallardo. What is logging good for? Tracing program execution during development Debugging Providing an audit trail for.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
EGEE-II INFSO-RI Enabling Grids for E-sciencE EGEE and gLite are registered trademarks Practicals on RGMA Valeria Ardizzone INFN.
Enterprise Library Caching Application Block Peter Provost Software Design Engineer Ron Jacobs Product Manager Scott Densmore Software Design Engineer.
Lecture XI: Logging and Monitoring CS 4593 Cloud-Oriented Big Data and Software Engineering.
Network Programming: Servers. Agenda l Steps for creating a server Create a ServerSocket object Create a Socket object from ServerSocket Create an input.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Input Characters from the Keyboard tMyn1 Input Characters from the Keyboard We have been using console output, but not console (keyboard) input. The main.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
© 2001 By Default! A Free sample background from Slide 1 Motivation CMW logging Real-Time Task CMW Server Logging thread.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
CS520 Web Programming Bits and Pieces of Web Programming (I) Chengyu Sun California State University, Los Angeles.
Logging Microservice Deep Dive
CVS, Logging, Development
Threads in Java Two ways to start a thread
Lecture 14 Throwing Custom Exceptions
Lecture 21 Sockets 1 (Not in D&D) Date.
Handling Errors in Web Applications
Logger, Assert and Invariants
CS1101: Programming Methodology Recitation 7 – Exceptions
RADE new features via JAVA
CS102 – Exceptions David Davenport Latest: May 2015
Testing and Exceptions
How to be a Good Developer
Hello Socket Programming TCP and UDP
XML-RPC a lightweight data communication protocol
Handling Exceptions.
Advanced Programming Behnam Hatami Fall 2017.
Partnership.
ATS Application Programming: Java Programming
Debugging & Logging.
Web Design & Development Lecture 7
Java Exception Very slightly modified from K.P. Chow
Java Exception Very slightly modified from K.P. Chow
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Exceptions.
Web APIs API = Application Programmer Interface
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
Family Map Server: Design and Implementation Tips
Exceptions.
Exception Handling and Event Handling
Exceptions and Exception Handling
Defensive Programming
Presentation transcript:

CS 240 – Advanced Programming Concepts Logging CS 240 – Advanced Programming Concepts

Java Logging Java has built-in support for logging Logs contain messages that provide information to Software developers (e.g., debugging) System administrators Customer support agents Programs send log messages to “loggers” There can be one or more Each message has a “level” SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST

Java Logging Loggers have a method for each message level that are used to enter messages in the log severe, warning, info, config, fine, finer, finest Rather than removing debugging log messages from the code, we leave them in Loggers can be configured to include or omit log messages based on their levels Logger.setLevel(level) method ALL (include all messages) OFF (omit all messages) SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST (include all messages at a particular level and higher)

Java Logging Each logger has one or more “handlers” associated with it Handlers represent destinations to which the log messages should be sent ConsoleHandler (sends messages to the console) FileHandler (sends messages to a file) SocketHandler (sends messages to a network socket) Like loggers, handlers can also be configured to include or omit log messages based on their levels Handler.setLevel(level) method ALL (include all messages) OFF (omit all messages) SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST (include all messages at a particular level and higher)

Java Logging Each handler has a “formatter” which defines the format used to encode its messages SimpleFormatter XMLFormatter

Programmatic Configuration public class ProgrammaticConfigurationExample { private static Logger logger; static { try { initLog(); } catch (IOException e) { System.out.println("Could not initialize log: " + e.getMessage()); } } private static void initLog() throws IOException { Level logLevel = Level.FINEST; logger = Logger.getLogger("ProgrammaticConfigurationExample"); logger.setLevel(logLevel); logger.setUseParentHandlers(false); Handler consoleHandler = new ConsoleHandler(); consoleHandler.setLevel(logLevel); // consoleHandler.setFormatter(new SimpleFormatter()); logger.addHandler(consoleHandler); FileHandler fileHandler = new FileHandler("log.txt", false); fileHandler.setLevel(logLevel); // fileHandler.setFormatter(new SimpleFormatter()); logger.addHandler(fileHandler); }

File Configuration Example import java.util.logging.Level; import java.util.logging.Logger; /** * Specify the configuration file by adding the following VM option * in the run configuration or you will get the default logging * configuration: * * -Djava.util.logging.config.file=logging.properties * */ public class FileConfigurationExample { private static Logger logger = Logger.getLogger("FileConfigurationExample"); }

Logging Configuration File handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler java.util.logging.ConsoleHandler.level=FINE java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter java.util.logging.FileHandler.level=FINE java.util.logging.FileHandler.pattern=log.txt # Write 10MB before rotating this file java.util.logging.FileHandler.limit=10000000 # Number of rotating files to be used java.util.logging.FileHandler.count=4 java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter # Log format to use java.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n .level=FINEST

Log Formatting Use format specifiers from the java.util.Formatter class Available variables to log: %1$ The date/time the message was created %2$ The method that called the log method %3$ The name of the logger %4$ The level the message was logged at %5$ The message %6$ The throwable

Logging Messages Logging messages with specific levels severe(message) Same for warning, info, config, fine, finer, finest log(level, message) Logging method enter/exit entering(className, methodName) exiting(className, methodName) Logged at FINER level Logging throwing an exception throwing(className, methodName, throwable) Logging catching an exception log(level, message, throwable)

Logging Message import java.util.logging.Level; import java.util.logging.Logger; /** * Specify the configuration file by adding the following VM option in the run configuration * or you will get the default logging configuration: * * -Djava.util.logging.config.file=logging.properties * */ public class FileConfigurationExample { private static Logger logger = Logger.getLogger("FileConfigurationExample"); public static void main(String [] args) { logger.info("This is my info log message."); logger.fine("This is my fine log message"); try { throw new Exception("An exception occurred"); } catch (Exception ex) { logger.severe("This is my exception message"); logger.log(Level.SEVERE, "This is my message logged with the exception", ex); } } }

Logging Messages import java.util.logging.*; public class Server { private static Logger logger = Logger.getLogger(”Server"); private void run() { logger.info("Initializing HTTP Server"); try { server = HttpServer.create(new InetSocketAddress(SERVER_PORT_NUMBER), MAX_WAITING_CONNECTIONS); } catch (IOException e) { logger.log(Level.SEVERE, e.getMessage(), e); return; } logger.info("Creating contexts"); server.createContext("/games/list", new ListGamesHandler()); server.createContext("/routes/claim", new ClaimRouteHandler()); logger.info("Starting HTTP Server"); server.start();

Logging Messages class ClaimRouteHandler implements HttpHandler { private static Logger = Logger.getLogger("ClaimRouteHandler"); @Override public void handle(HttpExchange exchange) throws IOException { logger.entering("ClaimRouteHandler", "handle"); try { ... logger.fine(reqData); } catch (IOException e) { logger.log(Level.SEVERE, e.getMessage(), e); logger.exiting("ClaimRouteHandler", "handle");

Log4J Java didn’t have a logging API until version 1.4 Before 1.4, we had to log with System.out.println(…) or System.err.println(…) Or, use an external framework Log4J became very popular and widely used before 1.4 Log4J is still more commonly used than the built-in java.util.logging API Log4J is very similar to java.util.logging