Logging With log4j - PowerPoint PPT Presentation

1 / 36
About This Presentation
Title:

Logging With log4j

Description:

The logger named 'com.foo' is a parent of the logger named 'com.foo.Bar' ... Choosing the best move in a game. Perfect Decision, Two person games ... – PowerPoint PPT presentation

Number of Views:314
Avg rating:3.0/5.0
Slides: 37
Provided by: yg67
Category:
Tags: agame | com | create | game | games | log4j | logging | max | own | your

less

Transcript and Presenter's Notes

Title: Logging With log4j


1
Logging With log4j
  • http//logging.apache.org/log4j/

2
Why Log?
3
The Concept of Log Levels
4
Who are the Players?
  • Logger (what)
  • Appender (where)
  • Layout (how)

5
Logger
  • Logger is a named entity, each named logger is a
    singelton.
  • Logger log Logger.getLogger(myLogger)

6
Logger
  • There is a hierarchy of loggers (determined by
    their names).
  • The logger named "com.foo" is a parent of the
    logger named "com.foo.Bar".
  • Similarly, "java" is a parent of "java.util" and
    an ancestor of "java.util.Vector".

7
Logger
  • At the top of the hierarchy is the root logger.
  • Logger root Logger.getRootLogger()

8
Logger
  • When you have a logger, you can use it to log
    messages at various levels.
  • logger.debug(a debug message)
  • logger.info(an info message)
  • logger.warn(a warning)
  • logger.error(an error)
  • logger.fatal(oh no.)

9
Logger
  • You can assign a level to a logger.
  • logger.setLevel(Level.INFO)
  • It will then ignore all messages of lower levels.

10
Logger
  • If there is no assigned level, the logger assumes
    the level of its parent.

11
Logger
  • Suggestion use the full class name as the logger
    name for that class
  • Logger log Logger.getLogger(oosd.examples.logge
    r.LoggingApp)

12
Appender
  • The destination of the logger.
  • You can write your own, but usually youll just
    use an existing one
  • ConsoleAppender
  • FileAppender
  • SocketAppender

13
Layout
  • How will the message look like.
  • Again, you can write your own, but usually use
    one of
  • SimpleLayout
  • PatternLayout
  • HTMLLayout
  • XMLLayout

14
Example
  • Logger log Logger.getLogger(my.logger)
  • log.setLevel(Level.ERROR)
  • log.addAppender(new ConsoleAppender(new
    SimpleLayout())
  • log.warn(This will not be shown)
  • log.fatal(This will)

15
Hierarchy
  • Level if there is no level assigned, look for
    level of parent.
  • Appender append to all appenders assigned to
    current logger, as well to appenders of all
    ancestros.
  • You can override this by setting
    logger.setAdditivity(false)

16
Log Configuration
  • BasicConfigurator
  • BasicConfigurator.configure()
  • BasicConfigurator.configure(Appender a)

17
Log Configuration
  • From file
  • PropertyConfigurator.configure(String filename)
  • DOMConfigurator.configure(String filename)
  • This reads the configuration from a file (either
    Properties or XML). See documentation for the
    configuration files formats.

18
Java Exceptions
19
Java Exceptions
  • Throws
  • Try
  • Catch
  • Finally

20
Java Exceptions - Throwable
21
Java Exceptions
  • Finally things we should do no matter what
    happened in the try block.

22
Finally inner working of superman
  • Try
  • wearUniform()
  • fly(whereTheBadGuyIs)
  • catch (CantWearUniformInPublicException pe)
  • //
  • catch (CantFlyNearCryptonException ce)
  • //
  • finally
  • // no matter what went wrong before, the
    world WILL be saved
  • saveTheWorld()

23
Finally the boring example
  • try
  • out new PrintWriter(new FileWriter(f.txt))
  • //
  • catch (FileNotFoundException fnf)
  • //
  • catch (IOException e)
  • // ..
  • finally
  • if (out ! null) out.close()

24
Creating your own exceptions
  • Extend Exception and define the appropriate
    constructors.

25
Any questions about exceptions?
  • Good. So now we can continue to an

26
Algorithm!
27
MinMax Algorithm
  • Choosing the best move in a game

28
Perfect Decision, Two person games
  • Can be represented as a game-tree.

29
Perfect Decision, Two person games
  • Two players
  • MAX and MIN
  • MAX moves first alternate turns thereafter.
  • Formal definition of game
  • Initial State how the game starts?
  • Successor Function what are the possible
    moves?
  • Terminal Test can we continue playing?
  • Utility Function how good is the position for
    the given player?
  • No one player has full control, must develop a
    strategy.

30
Minimax Algorithm Basics
MAX
3
MIN
0
3
2
MAX
7
0
9
3
6
2
MIN
6
5
7
2
1
3
9
0
4
5
2
Process of Backing up minimax decision Assumption
Both players are knowledgeable and play the
best possible move
31
Minmax Algorithm
  • Generate game tree to all terminal states.
  • Apply utility function to each terminal state.
  • Propagate utility of terminal states up one
    level.
  • MAXs turn MAX tries to maximize utility value.
  • MINs turn MIN minimizes utility value.
  • Continue backing-up values toward root, one layer
    at a time.
  • At root node, MAX chooses the value with highest
    utility.

32
Problem
  • Full games might be too large to generate
  • Solution generate tree up to a fixed depth, use
    a heuristic function to calculate the utility of
    the non-terminal leafs.

33
Example Partial Search for Tic-Tac-Toe
Position p win for Max, u(p) ? loss for MAX,
u(p) ? otherwise, u(p) ( of complete
rows, cols, diags still open for MAX) ( of
complete rows, cols, and diags still open for MIN)
34
Tic-Tac-Toe Move 2
35
MAX Player 3rd move
36
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com