001    package toolbus.logging;
002    
003    import java.io.PrintStream;
004    import java.text.SimpleDateFormat;
005    import java.util.Date;
006    
007    /**
008     * Logger implementation that writes to the command line / std-err.
009     * 
010     * @author Arnold Lankamp
011     */
012    public class CommandLineLogger implements ILogger{
013            private final int level;
014            private final PrintStream printStream;
015            private boolean timestamp = false;
016    
017            /**
018             * Default constructor. Inititalizes this logger with the default log level (WARNING).
019             */
020            CommandLineLogger(){
021                    this(ILogger.WARNING);
022            }
023    
024            /**
025             * Constructor. Initialized this logger with the given log level.
026             * 
027             * @param level
028             *            The log level to initialize this logger with.
029             */
030            CommandLineLogger(int level){
031                    super();
032    
033                    this.level = level;
034                    printStream = System.err;
035            }
036    
037            /**
038             * Method for checking if we should log a message. This depends on the log level and the given
039             * log level.
040             * 
041             * @param loglevel
042             *            The log level of the message.
043             * @return True if we should log the message with the given log level, false otherwise.
044             */
045            private boolean shouldLog(int loglevel){
046                    return (this.level >= loglevel);
047            }
048            
049            /* 
050             * @see toolbus.logging.ILogger#setTimestamp(boolean)
051             */
052            public void setTimestamp(boolean on) {
053                    timestamp = on;
054            }
055    
056            /**
057             * @see ILogger#log(String, int)
058             */
059            public void log(String message, int loglevel){
060                    if(shouldLog(loglevel)){
061                            PrintStream ps = printStream;
062                            synchronized(ps){
063                                    if(timestamp){
064                                            ps.print("[");
065                                            ps.print(getFormattedDateTime());
066                                            ps.print("] - ");
067                                    }
068                                    ps.print(getLogLevelString(loglevel));
069                                    ps.print(" - ");
070                                    ps.print(message);
071                                    ps.println();
072                            }
073                    }
074            }
075    
076            /**
077             * @see ILogger#log(String, Throwable, int)
078             */
079            public void log(String message, Throwable throwable, int loglevel){
080                    if(shouldLog(loglevel)){
081                            PrintStream ps = printStream;
082                            synchronized(ps){
083                                    if(timestamp){
084                                            ps.print("[");
085                                            ps.print(getFormattedDateTime());
086                                            ps.print("] - ");
087                                    }
088                                    ps.print(getLogLevelString(loglevel));
089                                    ps.print(" - ");
090                                    ps.print(message);
091                                    ps.println();
092                                    
093                                    throwable.printStackTrace(ps);
094                                    ps.println();
095                            }
096                    }
097            }
098    
099            /**
100             * Returns a properly formated string containing the date and time.
101             * 
102             * @return A properly formated string containing the date and time.
103             */
104            public static String getFormattedDateTime(){
105                    return new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z").format(new Date());
106            }
107    
108            /**
109             * Returns a string containing a string representation of the log level.
110             * 
111             * @param logLevel
112             *            The log level we want the string representation of.
113             * @return The string representation of the given log level.
114             */
115            public static String getLogLevelString(int logLevel){
116                    switch(logLevel){
117                            case ILogger.ERROR:
118                                    return ILogger.ERRORSTR;
119                            case ILogger.WARNING:
120                                    return ILogger.WARNINGSTR;
121                            case ILogger.FATAL:
122                                    return ILogger.FATALSTR;
123                            case ILogger.INFO:
124                                    return ILogger.INFOSTR;
125                            case ILogger.DEBUG:
126                                    return ILogger.DEBUGSTR;
127                            case ILogger.LOG:
128                                    return ILogger.LOGSTR;
129                            default:
130                                    return ILogger.UNKNOWNSTR;
131                    }
132            }
133    
134            
135    }