001    /*
002     @license.text@
003     */
004    package biz.hammurapi.logging;
005    
006    /**
007     * @author Pavel Vlasov 
008     * @version $Revision: 1.2 $
009     */
010    public class ConsoleLogger implements Logger {
011            public static final int DEBUG=0;
012            public static final int VERBOSE=1;
013            public static final int INFO=2;
014            public static final int WARN=3;
015            public static final int ERROR=4;
016            
017            private int level;
018            
019            /**
020             * Default constructor with INFO level.
021             */
022            public ConsoleLogger() {
023                    level=INFO;
024            }
025            
026            public ConsoleLogger(String level) {
027                    if ("DEBUG".equalsIgnoreCase(level)) {
028                            this.level=DEBUG;
029                    } else if ("VERBOSE".equalsIgnoreCase(level)) {
030                            this.level=VERBOSE;
031                    } else if ("INFO".equalsIgnoreCase(level)) {
032                            this.level=INFO;
033                    } else if ("WARN".equalsIgnoreCase(level)) {
034                            this.level=WARN;
035                    } else if ("ERROR".equalsIgnoreCase(level)) {
036                            this.level=ERROR;
037                    } else {
038                            throw new IllegalArgumentException("Invalid level: "+level);
039                    }
040            
041                    
042            }
043            
044            public ConsoleLogger(int level) {
045                    if (level<DEBUG || level>ERROR) {
046                            throw new IllegalArgumentException("Invalid level: "+level);
047                    }
048                    this.level=level;
049            }
050    
051            public void debug(Object source, String message) {
052                    if (level<=DEBUG) {
053                            System.out.println("[DEBUG] "+source+" "+message);
054                    }
055            }
056    
057            public void verbose(Object source, String message) {
058                    if (level<=VERBOSE) {
059                            System.out.println("[VERBOSE] "+source+" "+message);
060                    }
061            }
062    
063            public void info(Object source, String message) {
064                    if (level<=INFO) {
065                            System.out.println("[INFO] "+source+" "+message);
066                    }
067            }
068    
069            public void warn(Object source, String message) {
070                    if (level<=WARN) {
071                            System.out.println("[WARN] "+source+" "+message);
072                            if (source instanceof Throwable) {
073                                    ((Throwable) source).printStackTrace();
074                            }
075                    }
076            }
077    
078            public void error(Object source, String message) {
079                    if (level<=ERROR) {
080                            System.out.println("[ERROR] "+source+" "+message);
081                            if (source instanceof Throwable) {
082                                    ((Throwable) source).printStackTrace();
083                            }
084                    }
085            }
086    }