001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.io.log.log4j.layout; 020 021import java.util.Locale; 022import java.util.TimeZone; 023 024import lucee.commons.lang.ExceptionUtil; 025import lucee.commons.lang.StringUtil; 026import lucee.runtime.format.DateFormat; 027import lucee.runtime.format.TimeFormat; 028import lucee.runtime.op.Caster; 029 030import org.apache.log4j.Layout; 031import org.apache.log4j.spi.LoggingEvent; 032import org.apache.log4j.spi.ThrowableInformation; 033 034public class ClassicLayout extends Layout { 035 private static final String LINE_SEPARATOR=System.getProperty("line.separator"); 036 037 private static final DateFormat dateFormat=new DateFormat(Locale.US); 038 private static final TimeFormat timeFormat=new TimeFormat(Locale.US); 039 040 041 @Override 042 public String getContentType() { 043 // TODO Auto-generated method stub 044 return super.getContentType(); 045 } 046 047 @Override 048 public String getHeader() { 049 return "\"Severity\",\"ThreadID\",\"Date\",\"Time\",\"Application\",\"Message\""+LINE_SEPARATOR; 050 } 051 052 @Override 053 public void activateOptions() { 054 // TODO Auto-generated method stub 055 056 } 057 058 @Override 059 public String format(LoggingEvent event) { 060 061 StringBuilder data=new StringBuilder(); 062 String application; 063 064 String msg = Caster.toString(event.getMessage(),null); 065 int index=msg.indexOf("->"); 066 if(index>-1) { 067 application=msg.substring(0,index); 068 msg=msg.substring(index+2); 069 } 070 else 071 application=""; 072 073 //if(!ArrayUtil.isEmpty(params)) 074 // application=Caster.toString(params[0],""); 075 // Severity 076 data.append('"'); 077 data.append(event.getLevel().toString()); 078 data.append('"'); 079 080 data.append(','); 081 082 data.append('"'); 083 data.append(event.getThreadName()); 084 data.append('"'); 085 086 data.append(','); 087 088 // Date 089 data.append('"'); 090 091 data.append(dateFormat.format(event.timeStamp,"mm/dd/yyyy",TimeZone.getDefault())); 092 data.append('"'); 093 094 data.append(','); 095 096 // Time 097 data.append('"'); 098 data.append(timeFormat.format(event.timeStamp,"HH:mm:ss",TimeZone.getDefault())); 099 data.append('"'); 100 101 data.append(','); 102 103 // Application 104 data.append('"'); 105 data.append(StringUtil.replace(application,"\"","\"\"",false)); 106 data.append('"'); 107 108 data.append(','); 109 110 // Message 111 data.append('"'); 112 if(msg==null && event.getMessage()!=null) msg=event.getMessage().toString(); 113 data.append(StringUtil.replace(msg,"\"","\"\"",false)); 114 ThrowableInformation ti = event.getThrowableInformation(); 115 if(ti!=null) { 116 Throwable t = ti.getThrowable(); 117 data.append(';'); 118 String em = ExceptionUtil.getMessage(t); 119 data.append(StringUtil.replace(em,"\"","\"\"",false)); 120 data.append(';'); 121 String est = ExceptionUtil.getStacktrace(t, false); 122 data.append(StringUtil.replace(est,"\"","\"\"",false)); 123 } 124 125 data.append('"'); 126 127 return data.append(LINE_SEPARATOR).toString(); 128 129 } 130 131 132 @Override 133 public boolean ignoresThrowable() { 134 return false; 135 } 136 137}