
com.github.becausetesting.commonswindow.Log4jMessageAppender Maven / Gradle / Ivy
package com.github.becausetesting.commonswindow;
/*-
* #%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.JTextArea;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LocationInfo;
import org.apache.log4j.spi.LoggingEvent;
public class Log4jMessageAppender extends AppenderSkeleton {
private JTextArea JTextArea;
@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);
String level = event.getLevel().toString();
LocationInfo locationInfo=event.getLocationInformation();
String className=locationInfo.fullInfo;
String message = event.getRenderedMessage();
JTextArea.append(formatTime+" "+level+" "+className+"- "+message+"\n");
}
public JTextArea getJTextArea() {
return JTextArea;
}
/**
* @param jTextArea the jtextarea object
*/
public void setJTextArea(JTextArea jTextArea) {
JTextArea = jTextArea;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy