![JAR search and dependency download from the Maven repository](/logo.png)
com.github.becausetesting.commonswindow.Log4jMessageAppender Maven / Gradle / Ivy
package com.github.becausetesting.commonswindow;
import java.awt.Color;
/*-
* #%L
* commons-window
* $Id:$
* $HeadURL:$
* %%
* Copyright (C) 2016 Alter Hu
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.LoggingEvent;
import com.github.becausetesting.apache.commons.FilenameUtils;
public class Log4jMessageAppender extends AppenderSkeleton {
// private JTextArea JTextArea;
private JTextPane JTextPane;
@Override
public void close() {
// TODO Auto-generated method stub
}
@Override
public boolean requiresLayout() {
// TODO Auto-generated method stub
return false;
}
/*
* (non-Javadoc)
*
* @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.
* LoggingEvent)
*/
@Override
protected void append(LoggingEvent event) {
// TODO Auto-generated method stub
Date currentTime = Calendar.getInstance().getTime();
String formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(currentTime);
Level level = event.getLevel();
LocationInfo locationInfo = event.getLocationInformation();
String className = FilenameUtils.removeExtension(locationInfo.getFileName())+"("+locationInfo.getMethodName()+":"+locationInfo.getLineNumber()+")";
String message = formatTime + " " + className +" " + level.toString()+ " - " + event.getRenderedMessage()+"\n";
// JTextArea.append(formatTime+" "+level.toString()+" "+className+"-
// "+message+"\n");
Color color;
if (level == Level.ERROR) {
color = Color.RED;
} else if (level == Level.WARN || level == Level.TRACE) {
color = Color.BLUE;
} else {
color = Color.BLACK;
}
appendToPane(JTextPane, message, color);
}
public void appendToPane(JTextPane tp, String msg, Color c) {
StyleContext sc = StyleContext.getDefaultStyleContext();
AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c);
aset = sc.addAttribute(aset, StyleConstants.FontFamily, "Lucida Console");
aset = sc.addAttribute(aset, StyleConstants.Alignment, StyleConstants.ALIGN_JUSTIFIED);
int len = tp.getDocument().getLength();
/*tp.setCaretPosition(len);
tp.setCharacterAttributes(aset, false);
tp.replaceSelection(msg);*/
try {
tp.setCaretPosition(len);
tp.getDocument().insertString(len, msg, aset);
} catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public JTextPane getJTextPane() {
return JTextPane;
}
public void setJTextPane(JTextPane jTextPane) {
JTextPane = jTextPane;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy