I have the following code, but I am really struggling with log4j documentation.
How do I set append=false to my file appender, so I get a new file for each run?
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory
.newConfigurationBuilder();
builder.setStatusLevel(Level.ERROR);
builder.setConfigurationName("IkodaLogBuilder");
AppenderComponentBuilder appenderBuilder = ........
LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout").addAttribute("pattern",
"%d [%t] %-5level: %msg%n");
appenderBuilder = builder.newAppender("file", "File").addAttribute("fileName", "./logs/ikoda.log")
.add(layoutBuilder);
builder.add(appenderBuilder);
Related
I am trying to configure log4j2 programmatically and i am facing issue with logs coming from my cluster.
Here is the case, when user enable file logging (by passing log path as parameter in this case) all logs should go to File, no console logs. When use disable file logging, all logging should go to console. So I am trying in this:
public static void initializeLogging(
boolean vNewVerboseDebugLogging, String vLoggingPath, String vNewWarehouseQueriesLogPath,
boolean vNoConsoleLogging, boolean vIsEmbeddedHive, boolean isTestSubmission, boolean YarnLogs, boolean debugLogs) throws IOException
{
String[] packageGrp = {"org.apache","hive.ql","com.cdcb.cstone"};// this i am using to turn off logging from other packages/classes
if(vLoggingPath!=null) {
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
RootLoggerComponentBuilder rootLogger = builder.newRootLogger(debugLogs?Level.DEBUG:Level.INFO).addAttribute("additivity", true);
LayoutComponentBuilder layoutComponentBuilder = builder.newLayout("PatternLayout").addAttribute("pattern",
(isTestSubmission)?"%-5p [%t]: %m%n":"%d %-5p [%t]: %m%n");
AppenderComponentBuilder fileAppenderBuilder;
fileAppenderBuilder = builder.newAppender("LogToRollingFile", "RollingFile")
.addAttribute("fileName", vLoggingPath)
.addAttribute("filePattern", vLoggingPath + "-%d{MM-dd-yy-HH}.log")
.add(layoutComponentBuilder)
.addComponent(builder.newComponent("Policies")
.addComponent(builder.newComponent("TimeBasedTriggeringPolicy").addAttribute("interval", "1")));
/*builder.newLogger("com.cdcb.idn.HadoopCluster", (vVerboseDebugLogging)?Level.DEBUG:Level.INFO)
.add(builder.newAppenderRef("LogToRollingFile"))
.addAttribute("additivity", false);*/
for(String packNm: packageGrp)
addNewLoggerComponent(builder,packNm,builder.newAppenderRef("LogToRollingFile"),Level.OFF);
builder.add(fileAppenderBuilder);
rootLogger.add(builder.newAppenderRef("LogToRollingFile"));
builder.add(rootLogger);
Configurator.initialize(builder.build()).updateLoggers();
} else {
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
RootLoggerComponentBuilder rootLogger = builder.newRootLogger(debugLogs?Level.DEBUG:Level.INFO);
LayoutComponentBuilder layoutComponentBuilder = builder.newLayout("PatternLayout").addAttribute("pattern",
(isTestSubmission)?"%-5p [%t]: %m%n":"%d %-5p [%t]: %m%n");
AppenderComponentBuilder vConsoleInfo;
AppenderComponentBuilder vConsoleProblems;
if (!vIsEmbeddedHive){
vConsoleProblems = builder.newAppender("consoleProb", "CONSOLE")
.addAttribute("target", ConsoleAppender.Target.SYSTEM_ERR)
.add(layoutComponentBuilder);
builder.newLogger("com.cdcb.idn.HadoopCluster", (vVerboseDebugLogging)?Level.DEBUG:Level.INFO)
.add(builder.newAppenderRef("consoleProb"))
.addAttribute("additivity", false);
for(String packNm: packageGrp)
addNewLoggerComponent(builder,packNm,builder.newAppenderRef("consoleProb"),Level.OFF);
builder.add(vConsoleProblems);
rootLogger.add(builder.newAppenderRef("consoleProb"));
builder.add(rootLogger);
}
vConsoleInfo = builder.newAppender("consoleInfo", "CONSOLE")
.addAttribute("target", ConsoleAppender.Target.SYSTEM_OUT)
.add(layoutComponentBuilder);
/*builder.newLogger("com.cdcb.idn.HadoopCluster", (vVerboseDebugLogging)?Level.DEBUG:Level.INFO)
.add(builder.newAppenderRef("consoleInfo"))
.addAttribute("additivity", false);*/
for(String packNm: packageGrp)
addNewLoggerComponent(builder,packNm,builder.newAppenderRef("consoleInfo"),Level.OFF);
builder.add(vConsoleInfo);
rootLogger.add(builder.newAppenderRef("consoleInfo"));
builder.add(rootLogger);
Configurator.initialize(builder.build()).updateLoggers();
}
LOGGER = LogManager.getLogger(HadoopClusterLogging.class);
}
public static ConfigurationBuilder addNewLoggerComponent(ConfigurationBuilder builder, String name, AppenderRefComponentBuilder appenderReferences, Level level) {
return builder.add(builder.newLogger(name, level)
.add(appenderReferences)
.addAttribute("additivity", false));
}
but the issue here is, when file logging enabled, not all logs going to File, some going to console(for example, logs coming from hadoop cluster). What am i doing wrong here and how can i capture all logs to file when file logging enabled? can come one please help?
One more thing is, how can i disable transitive dependencies logging. Meaning, my logger should log my application logs only, not from from my dependencies.
Thank you!
Following is the code, I am working on :
Purpose is to configure SmtpAppender programmatically. Along with the SmtpAppender, I also need to add RollingFileAppender as well as Console appender programmatically.
package vish;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.appender.SmtpAppender;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
import org.apache.logging.log4j.core.config.builder.api.LayoutComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuilder;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
public class SmtpAppenderBuilder {
public static void main(String[] args) {
String pattern = "%d{MM-dd#HH\\:mm\\:ss}%-4r %-5p [%t] %37c %3x - %m%n";
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();
LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout").addAttribute("pattern", pattern);
RootLoggerComponentBuilder rootLogger = builder.newRootLogger(Level.DEBUG);
builder.setStatusLevel(Level.DEBUG);
org.apache.logging.log4j.core.appender.SmtpAppender.Builder smtpBuilder = SmtpAppender.newBuilder();
smtpBuilder.setName("emailAppender");
smtpBuilder.setSmtpUsername("test1#gmail.com");
smtpBuilder.setSmtpPassword("###YpSv1925");
smtpBuilder.setSmtpProtocol("https");
smtpBuilder.setSmtpHost("smtp.gmail.com");
smtpBuilder.setBufferSize(512);
smtpBuilder.setTo("test2#gmail.com");
smtpBuilder.setSubject("testing");
}
}
How should I add the smtpAppender to the configutation or the rootLogger ?
You are mixing up two APIs:
the ConfigurationBuilder API, which is the closest code equivalent to the configuration files. It only creates definitions of the actual logging components, the real ones are created when Configuration#initialize() is called on the configuration object. You can create the definition of an SMTPAppender like this:
private Configuration createConfig() {
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder()//
.setStatusLevel(Level.DEBUG);
LayoutComponentBuilder layoutBuilder = builder.newLayout("PatternLayout")//
.addAttribute("pattern", "%d{MM-dd#HH\\:mm\\:ss}%-4r %-5p [%t] %37c %3x - %m%n");
AppenderComponentBuilder appenderBuilder = builder.newAppender("emailAppender", "SMTP")//
.addAttribute("smtpUsername", "test1#gmail.com")
.addAttribute("smtpPassword", "###YpSv1925")
.addAttribute("smtpProtocol", "smtps")
.addAttribute("smtpHost", "smtp.gmail.com")
.addAttribute("to", "test2#gmail.com")
.addAttribute("subject", "testing")
.add(layoutBuilder);
AppenderRefComponentBuilder appenderRefBuilder = builder.newAppenderRef("emailAppender");
RootLoggerComponentBuilder rootLoggerBuilder = builder.newRootLogger(Level.DEBUG)//
.add(appenderRefBuilder);
return builder.add(appenderBuilder)//
.add(rootLoggerBuilder)
.build();
}
the actual builders of Log4j 2.x components, which are called by reflection by Configuration#initialize using the definitions above. You can also use them directly:
private static Configuration createConfig2() {
return new AbstractConfiguration(null, ConfigurationSource.NULL_SOURCE) {
#Override
protected void doConfigure() {
Layout<String> layout = PatternLayout.newBuilder()//
.withPattern("%d{MM-dd#HH\\:mm\\:ss}%-4r %-5p [%t] %37c %3x - %m%n")
.withConfiguration(this)
.build();
Appender appender = SmtpAppender.newBuilder()//
.setName("emailAppender")
.setSmtpUsername("test1#gmail.com")
.setSmtpPassword("###YpSv1925")
.setSmtpProtocol("smtps")
.setTo("test2#gmail.com")
.setSubject("testing")
.setLayout(layout)
.setConfiguration(this)
.build();
LoggerConfig rootLogger = getRootLogger();
rootLogger.setLevel(Level.DEBUG);
rootLogger.addAppender(appender, null, null);
}
};
}
Both Configurations are equivalent and you can apply them on the current context with:
Configurator.reconfigure(config);
However they will be lost upon Configurator.reconfigure(), unless you define your own ConfigurationFactory.
I have configuration for log4j2 in properties file. Logger sends output to the console but not to the file. It creates log file but it doesn't update it with content. What can be missing in my configuration file?
log4j2.properties
status = error
name = PropertiesConfig
property.filename = /usr/local/logs/kit.log
filters = threshold
filter.threshold.type = ThresholdFilter
filter.threshold.level = debug
appenders = rolling
appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${filename}
appender.rolling.filePattern = debug-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = %d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=10MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 20
loggers = rolling
logger.rolling.name = org.kit
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile
Test class
package org.kit.log;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class LogTest {
private static final Logger logger = LogManager.getLogger(LogTest.class);
public static void main(String[] args) {
logger.debug("Hello world - debug log");
logger.info("Hello world - info log");
logger.warn("Hello world - warn log");
logger.error("Hello world - error log");
}
}
You can try setting the first line in the configuration to status = TRACE to show Log4j2 internal logging on the console. This may help determine any problems with the configuration.
Also, can you try configuring a rootLogger instead of a named logger?
rootLogger.level = info
rootLogger.appenderRef.rolling.ref = RollingFile
I have this basic configuration of Log4J:
Logger log = Logger.getLogger(LogMessages.class.getName());
BasicConfigurator.configure(); // Basic configuration for Log4J 1.x
ConsoleAppender console = new ConsoleAppender(); //create appender
//configure the appender
String PATTERN = "%d [%p|%c|%C{1}] %m%n";
console.setLayout(new PatternLayout(PATTERN));
console.setThreshold(Level.FATAL);
console.activateOptions();
//add appender to any Logger (here is root)
Logger.getRootLogger().addAppender(console);
FileAppender fa = new FileAppender();
fa.setName("FileLogger");
fa.setFile("log" + File.separator + "messages.log");
fa.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
fa.setThreshold(Level.DEBUG);
fa.setAppend(true);
fa.activateOptions();
//add appender to any Logger (here is root)
Logger.getRootLogger().addAppender(fa); //repeat with all other desired appenders
// BASIC TEST
log.info("Some message 1");
log.info("Some message 2");
log.info("Some message 3");
log.info("Some message 4");
How I can configure Log4J to create a new file everyday and write messages into the file?
Use org.apache.log4j.DailyRollingFileAppender instead of FileAppender.
Details about settings can be found here.
EDIT
PatternLayout layout = new PatternLayout();
String conversionPattern = "[%p] %d %c %M - %m%n";
layout.setConversionPattern(conversionPattern);
// creates daily rolling file appender
DailyRollingFileAppender rollingAppender = new DailyRollingFileAppender();
rollingAppender.setFile("app.log");
rollingAppender.setDatePattern("'.'yyyy-MM-dd");
rollingAppender.setLayout(layout);
rollingAppender.activateOptions();
// configures the root logger
Logger rootLogger = Logger.getRootLogger();
rootLogger.setLevel(Level.DEBUG);
rootLogger.addAppender(rollingAppender);
// creates a custom logger and log messages
Logger logger = Logger.getLogger(My.class);
logger.debug("this is a debug log message");
logger.info("this is a information log message");
logger.warn("this is a warning log message");
I am trying to configure my application not to use log4j.properties, instead use BasicConfigurator to configure everything I have in my log4j.properties. I cannot figure out how to set the location of the file
I tried this with no luck.
Logger log = Logger.getLogger(pjp.getSignature().getDeclaringTypeName());
PatternLayout layout = new PatternLayout();
FileAppender familyservices = new FileAppender();
familyservices.setLayout(layout);
familyservices.setName("com.cambiahealth.engine.rest.family");
familyservices.setFile("/usr/regence/logs/datasvc/v1.0/familyservice.log");
log.addAppender(familyservices);
BasicConfigurator.configure(familyservices);
ConsoleAppender console = new ConsoleAppender(); //create appender
//configure the appender
String PATTERN = "%d [%p|%c|%C{1}] %m%n";
console.setLayout(new PatternLayout(PATTERN));
console.setThreshold(Level.FATAL);
console.activateOptions();
//add appender to any Logger (here is root)
Logger.getRootLogger().addAppender(console);
FileAppender fa = new FileAppender();
fa.setName("FileLogger");
fa.setFile("mylog.log");
fa.setLayout(new PatternLayout("%d %-5p [%c{1}] %m%n"));
fa.setThreshold(Level.DEBUG);
fa.setAppend(true);
fa.activateOptions();
/add appender to any Logger (here is root)
Logger.getRootLogger().addAppender(fa)
//repeat with all other desired appenders