Set specific level ThresholdFilter - java

I created 2 PatternLayoutEncoder in my program logger with different fields, 1 for info and debug messages and 2 for warning and error messages.
ThresholdFilter consoleFilter = new ThresholdFilter();
consoleFilter.setLevel("INFO");
consoleFilter.start();
consoleAppenderError.addFilter(consoleFilter);
ThresholdFilter consoleFilterError = new ThresholdFilter();
consoleFilterError.setLevel("WARN");
consoleFilterError.start();
consoleAppenderError.addFilter(consoleFilterError);
The info logger print also the warning and error messages, and I get double messages.
Is there an option to set specific level so that the info logger only receives info and debug messages without error and warning massages?

Related

I need to change the log level dynamically at runtime as per the log level set

I am using to invoke the below Java code in Mule 4.3 to try to set the level to debug but still printing INFO and other levels except Trace level. Basically its not updating all the loggers. Its working fine in Mule 3.8 with the below code in Expression component.
Java Code
LoggerContext context = (LoggerContext) LogManager.getContext(false);
Configuration config = context.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig("org.mule.runtime.core.internal.processor.LoggerMessageProcessor");
loggerConfig.setLevel(Level.DEBUG);
context.updateLoggers(config);
Console Output :
DEBUG 2022-08-17 22:33:50,499 [[MuleRuntime].uber.02: [dynamiclogger].dynamicloggerFlow1.CPU_LITE #112f86df] [processor: dynamicloggerFlow1/processors/0; event: 90dbce00-1e4e-11ed-a2e8-646e69d7e195] org.mule.runtime.core.internal.processor.LoggerMessageProcessor:
*********************DEBUG LEVEL*************************************
INFO 2022-08-17 22:33:50,499 [[MuleRuntime].uber.02: [dynamiclogger].dynamicloggerFlow1.CPU_LITE #112f86df] [processor: dynamicloggerFlow1/processors/1; event: 90dbce00-1e4e-11ed-a2e8-646e69d7e195] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: *************** INFO LEVEL ***************************

How to fix logger randomly logging on the same line, even though it is set to start each log with a new line

I am using log4j to log events in java code. I have it set to start each log line as new line, with timestamp, thread, log level and the class where the log runs. So the configuration looks like this:
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
logger = loggerContext.getLogger("com.asdf");
logger.setAdditive(true);
PatternLayoutEncoder encoder = new PatternLayoutEncoder();
encoder.setContext(loggerContext);
encoder.setPattern("%-5level %d [%thread:%M:%caller{1}]: %message%n");
encoder.start();
cucumberAppender = new CucumberAppender();
cucumberAppender.setName("cucumber-appender");
cucumberAppender.setContext(loggerContext);
cucumberAppender.setScenario(scenario);
cucumberAppender.setEncoder(encoder);
cucumberAppender.start();
logger.addAppender(cucumberAppender);
loggerContext.start();
logger().info("*********************************************");
logger().info("* Starting Scenario - {}", scenario.getName());
logger().info("*********************************************\n");
}
#After
public void showScenarioResult(Scenario scenario) throws InterruptedException {
logger().info("**************************************************************");
logger().info("* {} Scenario - {} ", scenario.getStatus(), scenario.getName());
logger().info("**************************************************************\n");
cucumberAppender.writeToScenario();
cucumberAppender.stop();
logger.detachAppender(cucumberAppender);
logger.detachAndStopAllAppenders();
}
which most of the times outputs the log correctly, as so:
15:59:25.448 [main] INFO com.asdf.runner.steps.StepHooks -
********************************************* 15:59:25.449 [main] INFO com.asdf.runner.steps.StepHooks - * Starting Scenario - Check Cache 15:59:25.450 [main] INFO com.asdf.runner.steps.StepHooks -
********************************************* 15:59:25.558 [main] DEBUG org.cache2k.core.util.Log - New instance, using SLF4J logging 15:59:25.575 [main] INFO org.cache2k.core.Cache2kCoreProviderImpl - cache2k starting. version=1.0.1.Final, build=undefined, defaultImplementation=HeapCache 15:59:25.629 [main] DEBUG org.cache2k.CacheManager:default - open name=default, id=wvl973, classloaderId=6us14y
However, sometimes the next line of the logger is written on the above one, without using the new line, like below:
15:59:27.353 [main] INFO com.asdf.cache.CacheService - Creating a cache for [Kafka] service with specific types.15:59:27.354 [main] INFO com.asdf.runner.steps.StepHooks - **************************************************************
15:59:27.354 [main] INFO com.asdf.runner.steps.StepHooks - * PASSED Scenario - Check Cache
15:59:27.354 [main] INFO com.asdf.runner.steps.StepHooks - **************************************************************
As you can see, the first StepHooks line goes on the same line as CacheService, which is unaesthetic.
What can i change in order for the log to always log in new line, without exceptions like this?

Java log4j No appenders could be found for logger

I am trying to make a custom logger and appender in log4j, but I am geting confusing error messages.
Here's the error:
[n.a.:n.a.] 19.Apr.2016 15:54 81 [ preRegister] ERROR stderr - log4j:WARN No appenders could be found for logger (datenImportLogger).
[n.a.:n.a.] 19.Apr.2016 15:54 81 [ preRegister] ERROR stderr - log4j:WARN Please initialize the log4j system properly.
[n.a.:n.a.] 19.Apr.2016 15:54 81 [ preRegister] ERROR stderr - log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Here's my config:
# datenImportLogger
log4j.logger.datenImportLogger=datenImportFileAppender
log4j.additivity.datenImportLogger=false
log4j.appender.datenImportFileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.datenImportFileAppender.File=java/log/datenimport.log
log4j.appender.datenImportFileAppender.MaxBackupIndex=5
log4j.appender.datenImportFileAppender.MaxFileSize=5MB
log4j.appender.datenImportFileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.datenImportFileAppender.layout.ConversionPattern=[%X{USER_CODE}:%X{CALLER_ID}] %d{dd.MMM.yyyy HH:mm:ss,SSS} - %m%n
Have I missed something?
Are there any debug Abilities which provide debug on runtime,
because -Dlog4j.debug is not posible.
Logger should be called in code like :
private static final Logger logger = Logger.getLogger("datenImportLogger");
Root Logger is already configured.
The line
log4j.logger.datenImportLogger=datenImportFileAppender is problematic, it miss the level definition
The logger definition should be:
log4j.logger.loggerName=[level|INHERITED|NULL], [appenderName1, appenderName2,...]
source: The Complete Log4j Manual
e.g.
log4j.logger.datenImportLogger=INFO, datenImportFileAppender
try placing log4j.debug=true in top of the log4.propeties file it should provide additional logging information (although it will have affect only after the configurations are parsed )

multiple log levels in same log file in log4j

How can multiple log levels added to the same log file in log4j?
For example:
log4j.rootLogger=INFO,WARN,stdout,file
It gives the log4j error when application start as:
Could not instantiate appender named WARN.
The purpose of the threshold is to tell log4j to ignore all logging requests with a priority lower than what you specify. Specifying a given threshold does not limit you to logging with that threshold.
FileAppender fa = new FileAppender();
fa.setThreshold(Level.INFO);
fa.setAppend(true);
Logger.getRootLogger().addAppender(fa);
In the above code, the appender has been configured to operate with a threshold of INFO. This means that the following code will not log, because DEBUG is a lower priority than INFO:
Logger logger = Logger.getLogger(SomeClass.class);
logger.debug("This will not log");
But this code will log:
logger.warn("This debug message will log.");
logger.error("And this error message will also log.");
In this case, both WARN and ERROR have a higher priority than INFO.

how to disable a log4j timestamp for only particular lines

i have logging configured in log4j.properties file , i am getting a output something like this
2013-11-12 19:33:17,897 - INFO Starting queue dispatching for DaphneStore Queue: om.fi
2013-11-12 19:33:17,897 - INFO Starting CBR queue dispatching for DaphneStore
2013-11-12 19:33:17,897 - INFO Starting server shutdown
is there any possibility that i do not get timestamp information on the left hand side for some of the lines, something like this
2013-11-12 19:33:17,897 - INFO Starting queue dispatching for DaphneStore Queue:
Starting CBR queue dispatching for DaphneStore
Starting server shutdown
here is my log4j configuration
# Set root logger to output only ERROR and FATAL events to R appender
log4j.rootLogger=ERROR, R
# Define R appender to output to local log
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.file=D:/logs/abc.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d %-5p %c - %m%n
Couple of quick solutions:
Create a different appender with ConversionPattern=%m%n (This will just print the logger text without timestamps I suppose).
Modify the exisitng appender with ConversionPattern=%m%n. Here you can manually add timestamps (using DateFormat like SimpleDateFormat) to the logger text wherever needed. In rest of the cases, plain text will be added to the logger file without timestamp.
You are probably not using log4j correctly. Every call to log.info/error/...() is considered a separate log message.
What you are probably trying to do is something like this:
final StringBuilder logmsg = new StringBuilder();
logmsg.append("Starting queue dispatching for DaphneStore Queue: \n");
logmsg.append("Starting CBR queue dispatching for DaphneStore\n");
logmsg.append("Starting server shutdown\n");
log.info(logmsg);
UPDATE
What I mean is that the timestamp is an important part of a log message, it says when something happened. If you don't need this information all the time you should probably lose the timestamp all-together and put a single logging call to mark when your application was started:
String moment = (new SimpleDateFormat("yyyy-MM-dd ...")).format(new Date());
log.info(moment + " Application started");

Categories