logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Blaine Bergeson (bbergeson)" <bberge...@micron.com>
Subject RE: Configuring Log4J without a file - FAQ needs update
Date Tue, 03 Feb 2015 19:00:57 GMT
John,

Here is an update to my previous solution in case it might be of help.  All of the following
methods are in a class called MyLogFactory.

    public static void logAppInfo(String appTitle, String appVersion, String logDir, String
logFilename) throws IOException {
        
        // the following properties are used by the configuration file to create the log file
in the correct location with the right name
        // and to write the information that I want on the first line(header) of the log file.
        System.setProperty(logDir, logDir);
        System.setProperty("app_title", appTitle);
        System.setProperty("app_version", appVersion);
        System.setProperty("logDir", logDir);
        System.setProperty("logFilename", logFilename);
        System.setProperty("newline", "\n");

        InputStream is = MyLogFactory.class.getResourceAsStream("log4j2.xml");
        ConfigurationSource source = new ConfigurationSource(is);

        // it seems that when I call LogManager.getLogger() in a class it depends upon the
LoggerContext and the context seems
        // to be different for each module; therefore I have to store the logger context returned
by intialize and then provide
        // a static method in this factory to return a logger based upon this context.  This
means that each class will need to
        // call the getLogger method in this class rather than calling LogManager.getLogger();
        _loggerContext = Configurator.initialize(STS_Log4j_Factory.class.getClassLoader(),
source);
    }

    public static Logger getLogger(String name) {
        return _loggerContext.getLogger(name);
    }
    
    public static void setLogLevel(String loggerName, Level newLevel) {
        Configuration config = _loggerContext.getConfiguration();
        LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
        loggerConfig.setLevel(newLevel);
        _loggerContext.updateLoggers();  // This causes all Loggers to refresh their information
from their LoggerConfig.
    }        

Blaine

-----Original Message-----
From: John Lussmyer [mailto:John.Lussmyer@efi.com] 
Sent: Tuesday, February 03, 2015 11:52 AM
To: Log4J Users List
Subject: RE: Configuring Log4J without a file - FAQ needs update

I can have a log "file" in my resources, but I can't write it to disk anywhere for Log4J to
then read.
So this solution does work for this case.
(and even if I couldn't have a resource file, I could just build a String and use a StringInputStream.)

-----Original Message-----
From: Blaine Bergeson (bbergeson) [mailto:bbergeson@micron.com] 
Sent: Tuesday, February 03, 2015 10:47 AM
To: Log4J Users List
Subject: RE: Configuring Log4J without a file - FAQ needs update

John,

This was the solution that I came up with to be able to load a configuration file that was
not on the class path.  I have since had to update the code to work correctly with a NetBeans
RCP application.  If I understand you correctly this solution will not work for you because
you don't want a configuration file at all while I did want a configuration file but I wanted
the file hidden within the project and not modified afterwards.

Blaine

-----Original Message-----
From: John Lussmyer [mailto:John.Lussmyer@efi.com] 
Sent: Tuesday, February 03, 2015 10:47 AM
To: log4j-user@logging.apache.org
Subject: Configuring Log4J without a file - FAQ needs update

I have a situation where I can't have a configuration file, so I want to configure Log4J programmatically.
The FAQ on this "How do I configure log4j2 in code without a configuration file?<http://logging.apache.org/log4j/2.x/faq.html#config_from_code>"
is rather unclear, as it suggests using a method that takes a file name.
Shouldn't it suggest the one that takes a ConfigurationSource?

I recently found this:
        InputStream is = MyLogFactory.class.getResourceAsStream("log4j2.xml");
        ConfigurationSource source = new ConfigurationSource(is);
        Configurator.initialize(MyLogFactory.class.getClassLoader(), source);

Is this the proper technique to use in this case?


________________________________
Confidentiality notice: This message may contain confidential information. It is intended
only for the person to whom it is addressed. If you are not that person, you should not use
this message. We request that you notify us by replying to this message, and then delete all
copies including any contained in your reply. Thank you.

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Mime
View raw message