Title: Logging With log4j
1Logging With log4j
- http//logging.apache.org/log4j/
2Why Log?
3The Concept of Log Levels
4Who are the Players?
- Logger (what)
- Appender (where)
- Layout (how)
5Logger
- Logger is a named entity, each named logger is a
singelton. - Logger log Logger.getLogger(myLogger)
6Logger
- 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".
7Logger
- At the top of the hierarchy is the root logger.
- Logger root Logger.getRootLogger()
8Logger
- 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.)
9Logger
- You can assign a level to a logger.
- logger.setLevel(Level.INFO)
- It will then ignore all messages of lower levels.
10Logger
- If there is no assigned level, the logger assumes
the level of its parent.
11Logger
- Suggestion use the full class name as the logger
name for that class - Logger log Logger.getLogger(oosd.examples.logge
r.LoggingApp)
12Appender
- The destination of the logger.
- You can write your own, but usually youll just
use an existing one - ConsoleAppender
- FileAppender
- SocketAppender
13Layout
- How will the message look like.
- Again, you can write your own, but usually use
one of - SimpleLayout
- PatternLayout
- HTMLLayout
- XMLLayout
-
14Example
- 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)
15Hierarchy
- 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)
16Log Configuration
- BasicConfigurator
- BasicConfigurator.configure()
- BasicConfigurator.configure(Appender a)
17Log 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.
18Java Exceptions
19Java Exceptions
20Java Exceptions - Throwable
21Java Exceptions
- Finally things we should do no matter what
happened in the try block.
22Finally 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()
23Finally the boring example
- try
- out new PrintWriter(new FileWriter(f.txt))
- //
- catch (FileNotFoundException fnf)
- //
- catch (IOException e)
- // ..
- finally
- if (out ! null) out.close()
24Creating your own exceptions
- Extend Exception and define the appropriate
constructors.
25Any questions about exceptions?
- Good. So now we can continue to an
26Algorithm!
27MinMax Algorithm
- Choosing the best move in a game
28Perfect Decision, Two person games
- Can be represented as a game-tree.
29Perfect 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.
30Minimax 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
31Minmax 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.
32Problem
- 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.
33Example 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)
34Tic-Tac-Toe Move 2
35MAX Player 3rd move
36(No Transcript)