001    package railo.commons.io.log;
002    
003    import java.util.Locale;
004    import java.util.TimeZone;
005    
006    import railo.commons.lang.StringUtil;
007    import railo.runtime.format.DateFormat;
008    import railo.runtime.format.TimeFormat;
009    import railo.runtime.op.Caster;
010    import railo.runtime.type.dt.DateTime;
011    import railo.runtime.type.dt.DateTimeImpl;
012    
013    /**
014     * Helper class for the logs
015     */
016    public final class LogUtil {
017        
018        private static final  DateFormat dateFormat=new DateFormat(Locale.US);
019        private static final  TimeFormat timeFormat=new TimeFormat(Locale.US);
020    
021        private static final String LINE_SEPARATOR=System.getProperty("line.separator");
022    
023        /**
024         * return log header line
025         * @return header
026         */
027        public static String getHeader() {
028            return "\"Severity\",\"ThreadID\",\"Date\",\"Time\",\"Application\",\"Message\""+LINE_SEPARATOR;
029        }
030            
031        /**
032         * return log line from given data
033         * @param type
034         * @param application
035         * @param message
036         * @return line
037         */
038        public static String getLine(int type,String application, String message) {
039            StringBuilder data=new StringBuilder();
040            if(application==null)application="";
041            if(message==null)message="";
042            
043            // Severity
044            data.append('"');
045            data.append(toStringType(type,"INFO"));
046            data.append('"');
047            
048            data.append(',');
049            data.append("\"web-0\"");
050            
051            data.append(',');
052            
053            DateTime date = new DateTimeImpl();
054            // Date
055            data.append('"');
056            data.append(dateFormat.format(date,"mm/dd/yyyy",TimeZone.getDefault()));
057            data.append('"');
058            
059            data.append(',');
060            
061            // Time
062            data.append('"');
063            data.append(timeFormat.format(date,"HH:mm:ss",TimeZone.getDefault()));
064            data.append('"');
065            
066            data.append(',');
067            
068            // Application
069            data.append('"');
070            data.append(StringUtil.replace(application,"\"","\"\"",false));
071            data.append('"');
072            
073            data.append(',');
074            
075            // Message
076            data.append('"');
077            data.append(StringUtil.replace(message,"\"","\"\"",false));
078            data.append('"');
079            
080            return data.append(LINE_SEPARATOR).toString();
081            
082        }
083        
084        /**
085         * translate int type to String type
086         * @param type
087         * @param defaultValue 
088         * @return string type
089         */
090        public static String toStringType(int type, String defaultValue) {
091            switch(type) {
092            case Log.LEVEL_INFO:    return "INFO"; 
093            case Log.LEVEL_DEBUG:   return "DEBUG"; 
094            case Log.LEVEL_WARN:    return "WARN"; 
095            case Log.LEVEL_ERROR:   return "ERROR"; 
096            case Log.LEVEL_FATAL:   return "FATAL"; 
097            default:                return defaultValue;
098            }
099        }
100        
101    
102        /**
103         * transalte a string log type to a int represenatation
104         * @param attribute
105         * @param defaultValue
106         * @return int lelog lev
107         */
108        public static int toIntType(String attribute, int defaultValue) {
109            if(attribute==null) return defaultValue;
110            attribute=attribute.toLowerCase().trim();
111            if(attribute.startsWith("info")) return Log.LEVEL_INFO;
112            if(attribute.startsWith("debug")) return Log.LEVEL_DEBUG;
113            if(attribute.startsWith("warn")) return Log.LEVEL_WARN;
114            if(attribute.startsWith("error")) return Log.LEVEL_ERROR;
115            if(attribute.startsWith("fatal")) return Log.LEVEL_FATAL;
116            
117            return defaultValue;
118        }    
119        
120            public static String toMessage(Exception e) {
121                    if(e==null) return "";
122                    String msg = e.getMessage();
123                    String clazz=Caster.toClassName(e);
124                    
125                    if(StringUtil.isEmpty(msg)) return clazz;
126                    
127                    return clazz+":"+msg;
128            }    
129        
130    /*
131        public static File getLogFileX(Log log) {
132            if(log instanceof LogFile) return ((LogFile)log).getFile();
133            return null;
134        }
135        public static Object getLogTemplate(Log log) {
136            if(log instanceof LogFile) return ((LogFile)log).getTemplate();
137            return "";
138        }
139    */
140    }