I'm an inexperienced Java developer writing an application that is handling a backup.
My application opens a gui (StepTwoBackup written using NetBeans Template), which gathers some information from the user, then when I press on the "Next" button, the gui passes these information to an object (BackupRestore which is logging all the operation using Log4J), and then opens another window (StepThreeBackup) and passes the object to it.
In this new window(StepThreeBackup), I automatically launch a method on the object passed (BackupRestore.execute()) which performs the backup.
In this last window (StepThreeBackup) I created a JTextArea where I would like to show the output of the Log4J (which currently writes to a log file and outputs to console).
Is there a way to do this? I've read that I should use an appender, but cannot figure out how to do this correctly.
For the time being I've created the following entry in my working Log4J property file:
<appender name="guiAppender" class="BackupAppGui.StatusMessageAppender">
<param name="Threshold" value="INFO" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss} %5p %c{1} - %m%n"/>
</layout>
</appender>
Then the following Class in my Package (following another post):
package BackupAppGui;
/**
*
* #author MSTPA
*/
import javax.swing.JTextArea;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.Level;
import org.apache.log4j.spi.LoggingEvent;
public class StatusMessageAppender extends AppenderSkeleton {
private final JTextArea jTextA;
public StatusMessageAppender() {
jTextA = StepThreeBackup.getJTextA();
}
protected void append(LoggingEvent event)
{
if(event.getLevel().equals(Level.INFO)){
jTextA.append(event.getMessage().toString());
}
}
public void close()
{
}
public boolean requiresLayout()
{
return false;
}
}
But nothing is written to the jTextArea.
What am I doing wrong? Can someone help me solving this? Thank you for all the help you can give me.
You need to make sure that the instance of JTextArea is not null. Yoy can try adding the appender programmatically (e.g. in the constructor of StepThreeBackup after create the components):
StatusMessageAppender appender = new StatusMessageAppender();
LogManager.getRootLogger().addAppender(appender);
Don't forget delete the entry in the log4j.xml file.
Related
I am trying to rotate my gc.log file every time my application starts up.
I am using this file appender in my logback.xml file.
...
<appender name="GCFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${log.directory}/gc.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${log.directory}/gc.log.%d{yyyyMMdd}_%d{HHmmss,aux}.gz</fileNamePattern>
<TimeBasedFileNamingAndTriggeringPolicy class="com.ga.omni.utility.StartupTriggeringPolicy" />
<maxHistory>50</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%msg%n</pattern>
</encoder>
</appender>
...
<root level="INFO">
<appender-ref ref="FILE" />
<appender-ref ref="GCFILE" />
</root>
(the "FILE" ref is a reference to our default logging file for the app.)
The appender references a TimeBasedFileNamingAndTriggeringPolicy named StartupTriggeringPolicy:
#NoAutoStart //won't be autostarted by Joran at config time
public class StartupTriggeringPolicy<E> extends DefaultTimeBasedFileNamingAndTriggeringPolicy<E> {
Logger log = LoggerFactory.getLogger(StartupTriggeringPolicy.class);
public StartupTriggeringPolicy() {
log.info("StartupTriggeringPolicy constructor called");
}
#Override
public void start() {
log.info("StartupTriggeringPolicy start() called... initialting gc.log rollover");
super.start();
//only check this once, on startup.
nextCheck = 0L;
isTriggeringEvent(null, null);
try {
tbrp.rollover();
log.info("StartupTriggeringPolicy start() called... gc.log successfully rolled over.");
} catch (RolloverFailure e) {
log.warn("Error rolling over gc.log file in StartupTriggeringPolicy.start()");
//Do nothing
}
}
}
The trouble that I'm facing is that the app starts up, but the StartupTriggeringPolicy never seems to get instantiated. None of the logs from the constructor or start() method are written, and if I put breakpoints in those methods, the breakpoints don't get hit.
Any suggestions would be greatly apperciated!
how to read property from properties file and use in log4j.xml if not found then use default path.
For an instance
<appender name="FILE" class="org.apache.log4j.FileAppender">
<param name="File" value="logs/${logfilename}.log" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d::[%t]::%-5p::%c::%x - %m%n" />
</layout>
</appender>
In above code snippet, I want to retrieve value of logfilename from properties file.If I mentioned logfilename=abc_log in property file then abc_log.log should be generated inside logs folder. If I am unable to find logfilename properties then by default location should be called for an instance /logs/default.log
could you please help in this, How I can achieve the above approach?
Below is a code for using Log4J for dynamically generate filename. It changes its name according to input file name and current date-time. (So helpful in case you run same file multiple times.) (Found this in another thread)
public class LogClass {
private static Logger log = Logger.getLogger(LogClass.class);
private static boolean initializationFlag = false;
private static String fileName;
private static void intializeLogger(){
log.setLevel(Level.DEBUG);
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date = new Date();
RollingFileAppender appender = new RollingFileAppender();
appender.setAppend(true);
appender.setMaxFileSize("1MB");
appender.setMaxBackupIndex(1);
appender.setFile(fileName + "_" + dateFormat.format(date) + ".log");
PatternLayout layOut = new PatternLayout();
layOut.setConversionPattern("%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n");
appender.setLayout(layOut);
log.addAppender(appender);
}
public static Logger getLogger(){
if(initializationFlag == false){
intializeLogger();
initializationFlag = true;
return LogClass.log;
}
else{
return LogClass.log;
}
}
public static void setFileName(String fileName){
LogClass.fileName = fileName;
}
We're using Log4net's ThreadContext.Stacks and it is mostly working well. My problem comes if there have been multiple ThreadContext.Stacks["key"].Push(...).
With a simple ConversionPattern:
<param name="ConversionPattern value="... topProp=%properties{key} ..."/>
I see log entries like:
... topProp=first second third ...
I'd really like to see only the most recently pushed value rather than all the values. I had hoped I could put something like the following in my appender/layout/ConversionPattern:
<param name="ConversionPattern value="... topProp=%properties{key}{1} ..."/>
but that doesn't work. I can kludge it by assuming/requiring all values to be the same length (say 5) and doing:
<param name="ConversionPattern value="... topProp=%5.5properties{key} ..."/>
But that isn't real attractive. Any ideas?
Thanks!
[Edit to add very simple example]
using System;
using System.IO;
using log4net;
using log4net.Config;
namespace ThreadLocalExample {
class Program {
private const string PropJobId = "Example:JobId";
static void Main() {
XmlConfigurator.Configure(new FileInfo("log4net.cfg"));
var log = LogManager.GetLogger(typeof(Program));
ThreadContext.Stacks[PropJobId].Push("Old");
log.Debug("Enter using");
using (ThreadContext.Stacks[PropJobId].Push("New")) {
log.Debug("stuff");
}
log.Debug("Out of using");
log.Debug("done.");
Console.ReadKey();
}
}
}
With the log4net configuration:
<appender name="Console" type="log4net.Appender.ConsoleAppender">
<threshold value="ALL" />
<layout type="log4net.Layout.PatternLayout">
<param name="ConversionPattern" value="[jobId=%P{Example:JobId}]: %m%n" />
</layout>
</appender>
Produces:
[jobId=Old]: Enter using
[jobId=Old New]: stuff
[jobId=Old]: Out of using
[jobId=Old]: done.
But I'd like:
[jobId=Old]: Enter using
[jobId=New]: stuff
[jobId=Old]: Out of using
[jobId=Old]: done.
I had the same problem, and it was not only about 'bad formatting', because I used database appender (AdoNetAppender) which expects integers. So after joining all stacked values together the result is not an integer any more. Consider an appender like this:
<appender name="DbAppender" type="log4net.Appender.AdoNetAppender">
...
<commandText value="INSERT INTO Log ([Id]) VALUES (#Id)" />
<parameter>
<parameterName value="#Id" />
<dbType value="Int32" />
<layout type="log4net.Layout.PatternLayout" value="%P{someId}" />
</parameter>
This appender will not accept any log message, where 'someId' is stacked twice or more - no logs in database ...
So, to solve this problem I gave up with stacks and turned back to flat properties.
I've coded some short extension:
public static class Log4NetExt {
public static IDisposable ThreadContextPush(string key, object value) {
object oldVal = ThreadContext.Properties[key];
ThreadContext.Properties[key] = value;
var topMostCleaner = new DispCleaner();
topMostCleaner.EvDispose += () => {
// Pop = restore old value
ThreadContext.Properties[key] = oldVal;
};
return topMostCleaner;
}
private class DispCleaner : IDisposable {
public event Action EvDispose;
public void Dispose() {
if (EvDispose != null) EvDispose();
}
}
}
And now, instead of:
using (ThreadContext.Stacks[PropJobId].Push("New")) {
write:
using (Log4NetExt.ThreadContextPush(PropJobId, "New")) {
and it works ok ;)
(This short code does not follow all best practises of building disposable objects, deleting event handlers and all this stuff, but it is short and I think it is safe in this simple case).
I have Done it via Filip solution modified as
public static class Log4NetExt
{
public static IDisposable ThreadContextSet(string key, object value)
{
//object oldVal = ThreadContext.Properties[key];
ThreadContext.Properties[key] = value;
var topMostCleaner = new DispCleaner();
topMostCleaner.EvDispose += () => {
// Pop = restore old value
//ThreadContext.Properties[key] = oldVal;
ThreadContext.Properties[key] = null;
};
return topMostCleaner;
}
private class DispCleaner : IDisposable
{
public event Action EvDispose;
public void Dispose()
{
if (EvDispose != null)
{
EvDispose();
}
}
}
}
And used it like
using (Log4NetExt.ThreadContextSet("ContextId", "Feed"))
using (Log4NetExt.ThreadContextPush("ContextValue", objFeed.FeedId))
{
}
I use Log4j to write some log my program.
I find and read many question and answer in this site, but i can't solve my problem.
Here my code:
1. log4j.xml
<appender name="rollingfileAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="append" value="true"/>
<param name="file" value="logs/process.log"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss:SSS} %-5p [%c{1}] %m%n"/>
</layout>
</appender>
<root>
<level value="DEBUG"/>
<appender-ref ref="rollingfileAppender"/>
<appender-ref ref="stdout"/>
</root>
2. My java code
package TestPacket;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
public class TestLog4jXML {
static Logger logger = org.apache.log4j.Logger.getLogger(TestLog4jXML.class.getName());
public TestLog4jXML() {
}
public static void main(String[] args) {
try {
DOMConfigurator.configure("log4j1.xml");
logger.trace("Entering application.");
logger.debug("Debug");
logger.info("info");
logger.warn("warn");
logger.error("error");
logger.fatal("fatal");
lungtng();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void lungtng()
{
logger.fatal("some text here");
logger.info("hello picaso");
}
}
And i run my program, with eclipse, windows os.
But the log file name only: process.log not in daily format: process.log.yyyy-MM-dd-HH
Who can explain this to me?
It appears that if you're running windows this is a known bug:
see here: http://do.whileloop.org/2014/02/14/log4j-rolling-file-appenders-in-windows/
See here: https://issues.apache.org/bugzilla/show_bug.cgi?id=29726
And also here: http://www.coderanch.com/t/424837/java/java/Log-log-file-rolled-day
The solutions seems to be to use the extras here:
http://logging.apache.org/log4j/extras/
Try using this appender with the correct policies defined:
http://logging.apache.org/log4j/extras/apidocs/org/apache/log4j/rolling/RollingFileAppender.html
The org.apache.log4j.DailyRollingFileAppender will create new log file for each day, each hour or each minute but file name of the current log always will be in the format that you've specified in the "file" parameter. In your example it's "process.log". The file names of the logs for the previous hours will be in format "process.log.yyyy-MM-dd-HH".
I've a custom log4j layout class that extends PatternLayout, my layout class simply masks the password in the log. It works in a simple console app. Here's the log4j.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="A1" class="org.apache.log4j.ConsoleAppender">
<layout class="com.PortalLog4jFilteringPattern"> <param name="ConversionPattern" value="%t %-5p %c{2} - %m%n"/> </layout>
</appender>
<root>
<priority value ="DEBUG" /> <appender-ref ref="A1" />
</root>
</log4j:configuration>
Here's a snipet of the layout class:
public class PortalLog4jFilteringPattern extends PatternLayout {
// omitted
#Override
public String format(LoggingEvent event) {
System.out.println("in format()...... ");
// rest omitted
Here's the calling code:
import org.apache.log4j.Logger;
public class ProductDemo {
private static Logger logger = Logger.getLogger(ProductDemo.class);
public ProductDemo() {
}
public void processOrder(CustomerOrder order) {
logger.info(order.getProductName());
}
// rest ommited
A sample result log with pswd being masked:
main INFO test.ProductDemo - "password":"*****"},
But once I moved the custom layout class to my webapp (log4j.xml is exactly the same.), it doesn't get called (i.e., no System.out output) and the pswd is still being shown. I'm running the webapp locally with maven on Jetty using this cmd: mvn jetty:run
Here's the calling code:
// original code, but I changed it to import org.apache.log4j.Logger for experiment
//import org.slf4j.LoggerFactory;
//import org.slf4j.Logger;
import org.apache.log4j.Logger;
public class BlahBlahClass extends Blah
// things omitted
private final static Logger log = Logger.getLogger( BlahBlahClass .class );
Any idea? thanks
In a Java EE server environment I would say: it's a class loader issue. Jetty is a servlet container, so it's class loading architecture is simpler; still, it's worth checking. If your log4j is not deployed within the WAR, but comes from the Jetty class path, this is almost certainly the cause.
Try changing the class loading strategy to "parent last", as described in the Jetty manual, see if it helps.
You should import package name of your class in log4j. I added to log4j2.xml as below:
<Configuration packages="package path of your class">