logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Daniel, Gillian" <gillian.dan...@petrotechnics.com>
Subject Custom Levels
Date Fri, 17 Sep 2004 09:49:09 GMT
Hi all,

Have been searching for information to assist me in my problem for a day
now, and I really cannot find anything which is helping me, so I thought
that perhaps someone else would be able to point me in the direction I
should be going.

 

We are using Weblogic 8.1.  We also use the commons-logging along side
the log4j.

 

We require a custom Level, so that we can output the messages with this
custom level to a different log file.  So I have created my custom
level:

 

 

package com.petrotechnics.xarc.log;

 

import org.apache.log4j.*;

 

public class SProLevel extends Level {

 

    public static final int ADMIN_INT = Level.FATAL_INT + 1;

    public final static String ADMIN_STR = "ADMIN";

    public final static SProLevel ADMIN = new SProLevel(ADMIN_INT,
ADMIN_STR, 5);

 

 

    public SProLevel(int level, String levelStr, int syslogEquivalent) {

        super(level, levelStr, syslogEquivalent);

 
System.out.println("CPLevel:CPLevel:level,levelStr,syslogEquivalent:\n"

                           + level + " " + levelStr + " " +
syslogEquivalent);

    } // end of SProLevel

 

    public static Priority toPriority(String sArg) {

        return toPriority(sArg, SProLevel.ADMIN);

    } // end of toPriority

 

    

    public static Priority toPriority(int i) throws
IllegalArgumentException {

        return toPriority(i, SProLevel.ADMIN);

    } // end of toPriority

 

    

    public static Priority toPriority(int i, Priority priority) {

        Priority p = (i == ADMIN_INT) ?

                SProLevel.ADMIN :

                Priority.toPriority(i, priority);

 

        return p;

    } // end of toPriority

 

    

    public static Priority toPriority(String sArg, Priority priority) {

        Priority p = (sArg == null) ?

                SProLevel.ADMIN :

                ((sArg.equalsIgnoreCase(SProLevel.ADMIN_STR)) ?

                SProLevel.ADMIN :

                Priority.toPriority(sArg, priority));

 

        return p;

    } // end of toPriority

 

    

    public static Level toLevel(String s) {

        return toLevel(s, SProLevel.ADMIN);

    } // end of toLevel

 

    

    public static Level toLevel(int i) {

        return toLevel(i, SProLevel.ADMIN);

    } // end of toLevel

 

 

    public static Level toLevel(int i, Level level) {

        Level l = (i == ADMIN_INT) ?

                SProLevel.ADMIN :

                Level.toLevel(i, level);

 

        return l;

    } // end of toLevel

 

    public static Level toLevel(String s, Level level) {

        Level l = (s == null) ?

                SProLevel.ADMIN :

                ((s.equalsIgnoreCase(SProLevel.ADMIN_STR)) ?

                SProLevel.ADMIN :

                Level.toLevel(s, level));

 

        return l;

    } // end of toLevel

 

 

    public static Priority[] getAllPossiblePriorities() {

        return new Priority[]{Priority.FATAL, Priority.ERROR,
Level.WARN,

                              Priority.INFO, Priority.DEBUG, ADMIN};

    } // end of getAllPossiblePriorities

 

} // end of SProAdminLevel

 

Then, so I could access this in the same manner that we log other
events, log.debug(""); I could do log.admin("");  I created my own
Logger:

 

package com.petrotechnics.xarc.log;

 

import org.apache.log4j.*;

 

public class SProLogger extends Logger {

 

    private static String FQCN = SProLogger.class.getName() + ".";

 

    private static SProLoggerFactory sproFactory = new
SProLoggerFactory();

 

 

    public SProLogger(String name) {

        super(name);

        System.out.println("SProLogger(): name: " + name);

    }

 

    public static Category getInstance(String name) {

        return Logger.getLogger(name, sproFactory);

    }

 

    public static Logger getLogger(String name) {

        return Logger.getLogger(name, sproFactory);

    }

 

    public void admin(Object message) {

        System.out.println("admin(): FQCN: " + FQCN);

        System.out.println("admin(): SProLevel.ADMIN: " +
SProLevel.ADMIN);

        System.out.println("admin(): message: " + message);

        super.log(FQCN, SProLevel.ADMIN, message, null);

    }

 

} // end of SProLogger

 

And of course my own logger factory:

 

package com.petrotechnics.xarc.log;

 

import org.apache.log4j.spi.LoggerFactory;

import org.apache.log4j.Logger;

 

public class SProLoggerFactory implements LoggerFactory {

 

 

    public SProLoggerFactory() {

    }

 

 

    public Logger makeNewLoggerInstance(String name) {

        return new SProLogger(name);

    }

 

} // end of SProLoggerFactory

 

Now, I have not integrated this with commons-logging, so when I create
an instance of this I call:

 

protected SProLogger adminLogger = new
SProLogger(this.getClass().getName());

 

And when I wish to write to the log file I call:

 

adminLogger.admin("Admin Logger Test");

 

I also have my log4j.properties:

 

# setup root logging to use CONSOLE, SPRO_LOG, LOG_MONITOR Appenders

log4j.rootLogger=ERROR, CONSOLE, SPRO_LOG, ADMIN_LOG

log4j.logger.com.petrotechnics.sentinelpro=DEBUG

log4j.logger.com.petrotechnics.sentinelpro.action=DEBUG

log4j.logger.com.petrotechnics.sentinelpro.application=DEBUG

#log4j.logger.com.petrotechnics.sentinelpro.domain=DEBUG

log4j.logger.com.petrotechnics.sentinelpro.dto=DEBUG

log4j.logger.com.petrotechnics.sentinelpro.rules=DEBUG

log4j.logger.com.petrotechnics.sentinelpro.service=ADMIN#com.petrotechni
cs.xarc.log.SProLevel

log4j.logger.com.petrotechnics.sentinelpro.service.admin=DEBUG

log4j.logger.com.petrotechnics.sentinelpro.service.audit=DEBUG

log4j.logger.com.petrotechnics.sentinelpro.service.build=DEBUG

log4j.logger.com.petrotechnics.sentinelpro.service.icc=DEBUG

log4j.logger.com.petrotechnics.sentinelpro.service.lesson=DEBUG

log4j.logger.com.petrotechnics.sentinelpro.service.rules=DEBUG

log4j.logger.com.petrotechnics.sentinelpro.service.startup=DEBUG

log4j.logger.com.petrotechnics.sentinelpro.service.user=DEBUG

log4j.logger.com.petrotechnics.sentinelpro.service.wcc=DEBUG

log4j.logger.com.petrotechnics.sentinelpro.ui=DEBUG

log4j.logger.com.petrotechnics.sentinelpro.util=DEBUG

log4j.logger.com.petrotechnics.xarc=DEBUG

log4j.logger.com.petrotechnics.xarc.security=DEBUG

log4j.logger.com.petrotechnics.xarc.controller=DEBUG

 

# Define Appender CONSOLE

# NOTE : altering any of these values will affect the console logging
output only

log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout

log4j.appender.CONSOLE.layout.ConversionPattern=[%p] [%c{1}] | %m%n

 

# Define Appender SPRO_LOG (Log File)

# NOTE : altering any of these values will affect the log file logging
output only

log4j.appender.SPRO_LOG=org.apache.log4j.RollingFileAppender

log4j.appender.SPRO_LOG.File=D:\\dev\\projects\\Sentinel Pro
v2\\web\\sentinel_pro\\WEB-INF\\log\\SentinelPro.log

log4j.appender.SPRO_LOG.Append=true

log4j.appender.SPRO_LOG.layout=org.apache.log4j.PatternLayout

log4j.appender.SPRO_LOG.layout.ConversionPattern=[%p] [%c{1}] | %m%n

# Set the appender to archive the file when it reaches 100KB

# and only keep one previous copy

log4j.appender.SPRO_LOG.MaxFileSize=100KB

log4j.appender.SPRO_LOG.MaxBackupIndex=1

 

# Define Appender ADMIN_LOG (Log File)

# NOTE : altering any of these values will affect the admin log file
logging output only

log4j.appender.ADMIN_LOG=org.apache.log4j.RollingFileAppender

log4j.appender.ADMIN_LOG.File=D:\\dev\\projects\\Sentinel Pro
v2\\web\\sentinel_pro\\WEB-INF\\log\\Admin.log

log4j.appender.ADMIN_LOG.Append=true

log4j.appender.ADMIN_LOG.layout=org.apache.log4j.PatternLayout

log4j.appender.ADMIN_LOG.layout.ConversionPattern=[%p] [%c{1}] | %m%n

# Set the appender to archive the file when it reaches 100KB

# and only keep one previous copy

log4j.appender.ADMIN_LOG.MaxFileSize=100KB

log4j.appender.ADMIN_LOG.MaxBackupIndex=1

 

# Define Appender LOG_MONITOR

# NOTE: Dev Environment only - REMOVE IN PRODUCTION ENVIRONMENTS

# log4j.appender.LOG_MONITOR=org.apache.log4j.net.SocketAppender

# log4j.appender.LOG_MONITOR.RemoteHost=localhost

# log4j.appender.LOG_MONITOR.Port=4445

# log4j.appender.LOG_MONITOR.LocationInfo=true

# webspherious.stdConversionPattern=%n%d{HH:mm:ss,SSS} %-5p at
%c.%M(%F:%L) %n    %m%n

 

And finally, I will put in the contents of my commons-logging.properties
file, incase this is related.

 

# Configure Commons Logging to use Log4j

org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.Lo
g4jFactory

 

When the adminLogger.admin("Admin Logger Test"); line is called, I get a
null pointer exception at org.apache.log4j.Category.log.

But when I start up my server, I get the following lines in my console
(so I am not surprised I get a null pointer, as it clearly cannot find
my custom level)

 

log4j:ERROR Could not find value for key
log4j.appender.ADMIN#com.petrotechnics.xarc.log.SProLevel

log4j:ERROR Could not instantiate appender named
"ADMIN#com.petrotechnics.xarc.log.SProLevel".

 

I am sorry this email is so long, as a rule, I do not like putting code
on mailing lists, but I have honestly no idea how else to explain what
is going wrong. 

I would be most grateful if anyone had any ideas of where I am going
wrong,  

Thanks in advance,

Gillian

 

 


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message