/* TestLog.java */

import java.io.*;
import org.apache.log4j.*;

class TestLog
{
    // global static vars for this class
    private  String version = "0.1, Oct. 23, 2004";
    static int xValue;
    static String name;
    static Logger logger = Logger.getLogger(TestLog.class);
    static PatternLayout myLogFormat = new PatternLayout("%d{} - %c %m%n");
    static DailyRollingFileAppender myLogFile;
    
    public TestLog()
    {
        xValue = 0;
        name = "dummy";
    }
    
    public String getVersion()
    {
        return this.version;
    }
    
    public void setXValue(int n)
    {
        //if (logger.isDebugEnabled()) logger.debug("setXValue("+n+")");
        xValue = n;
    }
    
    public void setName(String n)
    {
        //Logger l = Logger.getLogger(TestLog.class);
        //l.debug("setName("+n+")");
        name = n;
    }
    
    public int getXValue()
    {
        //Logger l = Logger.getLogger(TestLog.class);
        //l.debug("getXValue()="+this.getXValue());
        return xValue;
    }
    
    public String getName()
    {
        //Logger l = Logger.getLogger(TestLog.class);
        //l.debug("getName()="+this.getName());
        return name;
    }
    
    public static void main(String args[])
    {
        TestLog myInstance = new TestLog();
        //System.out.println("TestLog "+myInstance.getVersion());
        
        myLogFormat.setConversionPattern("%d{dd MMM yyyy HH:mm:ss,SSS} - %c %m%n");
        
        try
        {
            myLogFile = new DailyRollingFileAppender(myLogFormat, "LOG", "yyyy-MM-dd");
        }
        catch (IOException e)
        {
            System.err.println(e.toString());
        }
        
        //System.out.println("Log filename = "+myLogFile.getFile()+" append  = "+myLogFile.getAppend());
        
        //BasicConfigurator.configure();
        logger.addAppender(myLogFile);
        
        logger.setLevel(Level.INFO);
        
        boolean test =true;
        if (test)
        {
            logger.info("INFO level");
            logger.warn("Warn level");
            logger.debug("Debug level");
            logger.error("Error level");
            logger.fatal("Fatal level of logging");
        }
        int y = myInstance.getXValue();
        //System.out.println("Original = "+y);
        myInstance.setXValue(50);
        //System.out.println("After="+myInstance.getXValue());
    }
}

Start

javalog4jexample.txt · Last modified: 2014/10/26 01:52 (external edit)
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0