Skip to content

Commit

Permalink
checking in first crack at files
Browse files Browse the repository at this point in the history
  • Loading branch information
icfantv committed Nov 10, 2011
0 parents commit 17374f0
Show file tree
Hide file tree
Showing 2 changed files with 185 additions and 0 deletions.
154 changes: 154 additions & 0 deletions FooLayoutWrappingEncoder.java
@@ -0,0 +1,154 @@
package com.foo.logging;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
import org.apache.commons.lang3.CharUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class FooLayoutWrappingEncoder extends LayoutWrappingEncoder<ILoggingEvent> {

public static final long PRINT_TIME_INTERVAL = 1000L;

// Attribute codes:
// 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
//
// Text color codes:
// 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white

public static final String COLOR_YELLOW = "\033[1;33m";
public static final String COLOR_NEUTRAL = "\033[0m";
public static final String COLOR_ALL = "\033[0m";
public static final String COLOR_ERROR = "\033[01;31m";
public static final String COLOR_WARN = "\033[01;35m";
public static final String COLOR_INFO = "\033[01;34m";
public static final String COLOR_DEBUG = "\033[01;36m";
public static final String COLOR_TRACE = "\033[0;37m";
public static final String COLOR_DEFAULT = "\033[0m";

public FooLayoutWrappingEncoder() {
super();
this.setCharset(Charset.forName("UTF-8"));
this.clientImpl = new ClientImpl();
}

@Override
public void doEncode(ILoggingEvent event) throws IOException {

try {
this.maybeLogTime(this.clientImpl);
}
catch (IOException ex) {
// throw new RuntimeException(ex);
}

byte[] bytes = null;
String message = this.format(event);

This comment has been minimized.

Copy link
@tony19

tony19 Jan 18, 2012

Change this to:

this.format(event, getLayout());

try {
bytes = message.toString().getBytes(this.getCharset().name());
}
catch (UnsupportedEncodingException ex) {
throw new IOException(ex);
}

this.outputStream.write(bytes);
this.outputStream.flush();
}

private static String getTime() {
return FORMAT.format(Calendar.getInstance().getTime());
}

private static String format(ILoggingEvent event) {

This comment has been minimized.

Copy link
@tony19

tony19 Jan 18, 2012

Add the layout parameter here:

private static String format(ILoggingEvent event, Layout layout) {


StringBuilder sb = new StringBuilder(128);
sb.append(CharUtils.LF);

Level level = event.getLevel();
switch (level.toInt()) {
case Level.ALL_INT:
sb.append(COLOR_ALL);
break;
case Level.ERROR_INT:
sb.append(COLOR_ERROR);
break;
case Level.WARN_INT:
sb.append(COLOR_WARN);
break;
case Level.INFO_INT:
sb.append(COLOR_INFO);
break;
case Level.DEBUG_INT:
sb.append(COLOR_DEBUG);
break;
case Level.TRACE_INT:
sb.append(COLOR_TRACE);
break;
}

sb.append("[");
sb.append(level.toString());
sb.append("] - ");
sb.append(event.getFormattedMessage());

This comment has been minimized.

Copy link
@tony19

tony19 Jan 18, 2012

This call to event.getFormattedMessage() does not actually do what you expect in this case. To expand the pattern layout here, you need to use Layout.doLayout(event) to get the result.

So change this line to:

sb.append(layout.doLayout(event));

sb.append(COLOR_DEFAULT);

return sb.toString();
}

private static final DateFormat FORMAT =
new SimpleDateFormat("yyyy.MM.dd, hh:mm:ss a");

private static long lastLogTime = 0L;

private void maybeLogTime(ClientImpl client) throws IOException {

long now = System.currentTimeMillis();
if ((now - client.getLastLogTime()) >= PRINT_TIME_INTERVAL) {
logTime();
client.setLastLogTime(now);
}
}

private void logTime() throws IOException {

StringBuilder sb = new StringBuilder();
sb.append(COLOR_YELLOW);
sb.append(getTime()).append(":");
sb.append(COLOR_NEUTRAL);

byte[] bytes = null;
try {
bytes = sb.toString().getBytes(this.getCharset().name());
}
catch (UnsupportedEncodingException ex) {
throw new IOException(ex);
}

this.outputStream.write(bytes);
this.outputStream.flush();
}

private ClientImpl clientImpl;

public interface Client {
public long getLastLogTime();
public void setLastLogTime(long time);
}

private static class ClientImpl implements Client {

public long getLastLogTime() {
return lastLogTime;
}

public void setLastLogTime(long time) {
lastLogTime = time;
}
}
}
31 changes: 31 additions & 0 deletions logback.xml
@@ -0,0 +1,31 @@
<configuration>

<appender name="foouilog" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>/usr/local/tomcat/logs/fooui.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<!-- rollover daily -->
<fileNamePattern>/usr/local/tomcat/logs/fooui-%d{yyyy-MM-dd}.%i.gz</fileNamePattern>
<maxHistory>10</maxHistory>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>25MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder class="com.twc.logging.FooLayoutWrappingEncoder"/>
</appender>

<logger name="org.springframework">
<level value="info"/>
<appender-ref ref="foouilog"/>
</logger>

<logger name="java.sql">
<level value="warn"/>
<appender-ref ref="foouilog"/>
</logger>

<root level="INFO">
<appender-ref ref="foouilog"/>
</root>

</configuration>

0 comments on commit 17374f0

Please sign in to comment.