LOG4J1.x to LOG4J2 - java-ee-6

I would like to check on the following piece of code where I am stuck at from migrating from LOG4J1.X to LOG4J2.
File f = new File(fullFilePath);
Enumeration apps = logger.getAllAppenders();
if(!f.exists() || !apps.hasMoreElements())
{
logger.removeAllAppenders();
appender = new FileAppender();
appender.setName(fileName);
logger.addAppender(appender);
logger.setLevel(Level.toLevel(level));
}
I could convert the rest of the code successfully to log4j2, however the removeAllAppenders, getAllAppenders, addAppender, setLevel functions are not available under log4j2. Hence, how do I go about replacing them?

In Log4j2 Loggers don't have Appenders or Filters directly attached to them. Instead, they are associated with a LoggerConfig. The LoggerConfig does have a setLevel method but updating it won't actually modify the level of any Loggers that use it. To do that you have to call the updateLoggers method of the LoggerContext.
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
Configuration config = loggerContext.getConfiguration();
// Note that the next method gets the LoggerConfig that best matches the
// Logger name. If no logger
LoggerConfig loggerConfig = config.getLoggerConfig(loggerName);
Configurator.setLevel(loggerConfig, newLevel);
loggerContext.updateLoggers();
As far as the Appenders go, Appenders are not directly attached to a LoggerConfig. Instead, they are wrapped by an AppenderControl. You can get this list of Appenders attached to a LoggerConfig by calling getAppenderRefs. You can then remove each appender one by one.
However, doing this isn't recommended. Adding and removing Appenders like this is really only there to support unit testing of Log4j. The problem is that while you are doing this the system is still logging and you are going to end up in a state where log events are lost because no appenders are configured. The recommended approach in Log4j is to create a new configuration and then do
Configurator.initialize(Configuration);

Related

Can't redirect Trace.WriteLine to Serilog using SerilogTraceListener

In one of my project (.net core 3.1), I need a way to redirect System.Diagnostics.Trace.WriteLine to Serilog file. I found the SerilogTraceListener package which seems to be the right candidate.
Unfortunately until now, I haven't been able to find a way to make it works.
To reproduce it,
1) Create a .net core console project
2) Add the following nuget package : Serilog, SerilogTraceListener, Serilog.Sink.Console, Serilog.Sink.File
3) Overwrite the Program class code by the following
class Program
{
static void Main(string[] args)
{
// Works fine
Log.Logger = new LoggerConfiguration()
.WriteTo.Console()
.WriteTo.File("log.txt")
.CreateLogger();
Log.Logger.Information("A string written using Logger.Information");
// Trace is written in the console but not in the file
Trace.Listeners.Add(new ConsoleTraceListener());
Trace.Listeners.Add(new global::SerilogTraceListener.SerilogTraceListener());
System.Diagnostics.Trace.WriteLine("A string written using Trace.WriteLine");
}
}
What am I doing wrong?
TL;DR; You need to set the MinimumLevel to Debug or Verbose in order to see the Trace.WriteLine messages.
SerilogTraceListener maps System.Diagnostic.TraceEventType to Serilog.LogEventLevel, and when you call Trace.WriteLine, it maps these events to the Debug log event level.
That means Serilog's logger is receiving a message of type LogEventLevel.Debug.
The minimum level configured in Serilog by default is Information, which means Debug messages are being suppressed.
You have to configure the MinimumEventLevel to Debug (or Verbose) in order to see the Trace.WriteLine messages:
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug() // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
.WriteTo.Console()
.WriteTo.File("log.txt")
.CreateLogger();

Disable logging on FileConfigurationSourceChanged - LogEnabledFilter

I want Administrators to enable/disable logging at runtime by changing the enabled property of the LogEnabledFilter in the config.
There are several threads on SO that explain workarounds, but I want it this way.
I tried to change the Logging Enabled Filter like this:
private static void FileConfigurationSourceChanged(object sender, ConfigurationSourceChangedEventArgs e)
{
var fcs = sender as FileConfigurationSource;
System.Diagnostics.Debug.WriteLine("----------- FileConfigurationSourceChanged called --------");
LoggingSettings currentLogSettings = e.ConfigurationSource.GetSection("loggingConfiguration") as LoggingSettings;
var fdtl = currentLogSettings.TraceListeners.Where(tld => tld is FormattedDatabaseTraceListenerData).FirstOrDefault();
var currentLogFileFilter = currentLogSettings.LogFilters.Where(lfd => { return lfd.Name == "Logging Enabled Filter"; }).FirstOrDefault();
var filterNewValue = (bool)currentLogFileFilter.ElementInformation.Properties["enabled"].Value;
var runtimeFilter = Logger.Writer.GetFilter<LogEnabledFilter>("Logging Enabled Filter");
runtimeFilter.Enabled = filterNewValue;
var test = Logger.Writer.IsLoggingEnabled();
}
But test reveals always the initially loaded config value, it does not change.
I thought, that when changing the value in the config the changes will be propagated automatically to the runtime configuration. But this isn't the case!
Setting it programmatically as shown in the code above, doesn't work either.
It's time to rebuild Enterprise Library or shut it down.
You are right that the code you posted does not work. That code is using a config file (FileConfigurationSource) as the method to configure Enterprise Library.
Let's dig a bit deeper and see if programmatic configuration will work.
We will use the Fluent API since it is the preferred method for programmatic configuration:
var builder = new ConfigurationSourceBuilder();
builder.ConfigureLogging()
.WithOptions
.DoNotRevertImpersonation()
.FilterEnableOrDisable("EnableOrDisable").Enable()
.LogToCategoryNamed("General")
.WithOptions.SetAsDefaultCategory()
.SendTo.FlatFile("FlatFile")
.ToFile(#"fluent.log");
var configSource = new DictionaryConfigurationSource();
builder.UpdateConfigurationWithReplace(configSource);
var defaultWriter = new LogWriterFactory(configSource).Create();
defaultWriter.Write("Test1", "General");
var filter = defaultWriter.GetFilter<LogEnabledFilter>();
filter.Enabled = false;
defaultWriter.Write("Test2", "General");
If you try this code the filter will not be updated -- so another failure.
Let's try to use the "old school" programmatic configuration by using the classes directly:
var flatFileTraceListener = new FlatFileTraceListener(
#"program.log",
"----------------------------------------",
"----------------------------------------"
);
LogEnabledFilter enabledFilter = new LogEnabledFilter("Logging Enabled Filter", true);
// Build Configuration
var config = new LoggingConfiguration();
config.AddLogSource("General", SourceLevels.All, true)
.AddTraceListener(flatFileTraceListener);
config.Filters.Add(enabledFilter);
LogWriter defaultWriter = new LogWriter(config);
defaultWriter.Write("Test1", "General");
var filter = defaultWriter.GetFilter<LogEnabledFilter>();
filter.Enabled = false;
defaultWriter.Write("Test2", "General");
Success! The second ("Test2") message was not logged.
So, what is going on here? If we instantiate the filter ourselves and add it to the configuration it works but when relying on the Enterprise Library configuration the filter value is not updated.
This leads to a hypothesis: when using Enterprise Library configuration new filter instances are being returned each time which is why changing the value has no effect on the internal instance being used by Enterprise Library.
If we dig into the Enterprise Library code we (eventually) hit on LoggingSettings class and the BuildLogWriter method. This is used to create the LogWriter. Here's where the filters are created:
var filters = this.LogFilters.Select(tfd => tfd.BuildFilter());
So this line is using the configured LogFilterData and calling the BuildFilter method to instantiate the applicable filter. In this case the BuildFilter method of the configuration class LogEnabledFilterData BuildFilter method returns an instance of the LogEnabledFilter:
return new LogEnabledFilter(this.Name, this.Enabled);
The issue with this code is that this.LogFilters.Select returns a lazy evaluated enumeration that creates LogFilters and this enumeration is passed into the LogWriter to be used for all filter manipulation. Every time the filters are referenced the enumeration is evaluated and a new Filter instance is created! This confirms the original hypothesis.
To make it explicit: every time LogWriter.Write() is called a new LogEnabledFilter is created based on the original configuration. When the filters are queried by calling GetFilter() a new LogEnabledFilter is created based on the original configuration. Any changes to the object returned by GetFilter() have no affect on the internal configuration since it's a new object instance and, anyway, internally Enterprise Library will create another new instance on the next Write() call anyway.
Firstly, this is just plain wrong but it is also inefficient to create new objects on every call to Write() which could be invoked many times..
An easy fix for this issue is to evaluate the LogFilters enumeration by calling ToList():
var filters = this.LogFilters.Select(tfd => tfd.BuildFilter()).ToList();
This evaluates the enumeration only once ensuring that only one filter instance is created. Then the GetFilter() and update filter value approach posted in the question will work.
Update:
Randy Levy provided a fix in his answer above.
Implement the fix and recompile the enterprise library.
Here is the answer from Randy Levy:
Yes, you can disable logging by setting the LogEnabledFiter. The main
way to do this would be to manually edit the configuration file --
this is the main intention of that functionality (developers guide
references administrators tweaking this setting). Other similar
approaches to setting the filter are to programmatically modify the
original file-based configuration (which is essentially a
reconfiguration of the block), or reconfigure the block
programmatically (e.g. using the fluent interface). None of the
programmatic approaches are what I would call simple – Randy Levy 39
mins ago
If you try to get the filter and disable it I don't think it has any
affect without a reconfiguration. So the following code still ends up
logging: var enabledFilter = logWriter.GetFilter();
enabledFilter.Enabled = false; logWriter.Write("TEST"); One non-EntLib
approach would just to manage the enable/disable yourself with a bool
property and a helper class. But I think the priority approach is a
pretty straight forward alternative.
Conclusion:
In your custom Logger class implement a IsLoggenabled property and change/check this one at runtime.
This won't work:
var runtimeFilter = Logger.Writer.GetFilter<LogEnabledFilter>("Logging Enabled Filter");
runtimeFilter.Enabled = false/true;

How to set arrays of string to #EnableJpaRepositories from property files

I have a jpa configuration file with #EnableJpaRepositories annotaion. I set this annotaion value from application.properties file like this :
#EnableJpaRepositories("${jpa.repository.packages}")
public class JPAConfiguration {
....
}
and here is my application.properties file:
jpa.repository.packages=com.epms.model
and it works perfect. but i want to specify multiple packages for #EnableJpaRepositories . so i changed my config file to this :
jpa.repository.packages=com.epms.model,com.ecms.model
and also configuration file to this :
#EnableJpaRepositories("#{'${jpa.repository.packages}'.split(',')}")
public class JPAConfiguration {
}
but it's not working . any idea ? how can i do this in my configuration file?
As #amicoderozer is asking, if your classes share a common base package you only must indicate that root package.
If it's not your case (despite you are loading from a config file or you are declaring them manually) maybe the problem (will help posting any Exception or Runtime trace) is the way the split method is used. It returns an array, and I guess the generated code will be like this:
#EnableJpaRepositories("jpa.repository.packages1","jpa.repository.packages2")
That code doesn't compile.
Never tried Spring EL inside the annotation of a component, but despite this, maybe you should indicate the basePackages this way:
#EnableJpaRepositories(basePackages = "#{'${jpa.repository.packages}'.split(',')}")
If doesn't work, I recomend you first test it by manual array declaration:
#EnableJpaRepositories(basePackages = { "com.epms.model","com.ecms.model" })
Be sure all works as you expect, and then try again reading and parsing from config file.
UPDATE:
After some readings, I've concluded that is not possible do what you want. The SpEL is allowed in many places but for annotations there is only documentation and working examples with #Value annotation.

Grails mail plugin runtime configuration

Using grails mail plugin 1.0.7.
https://jira.grails.org/browse/GPMAIL-36 states that it's possible to change plguin configuration since 1.0.1 at runtime. Sadly it does not explains how to achieve it.
I want to be able to change the username at runtime to be able to use different mail accounts.
Thanks.
Based on this code, you should be able to change the configuration at runtime and the mail plugin will automagically re-deploy and update mail sender based on your changes.
Example:
Holders.config.grails.mail.username = 'foo'
Holders.config.grails.mail.password = 'bar'
sendMail {
to "foo#bar.com"
from "bar#foo.com"
subject "Hi"
body "This is an email"
}
Update:
It would appear that changing the configuration in this manner does not, in fact, fire the onConfigChange event. Per this, you can fire the event manually. Something like this:
Holders.pluginManager.getGrailsPlugin('mail').notifyOfEvent(GrailsPlugin.EVENT_ON_CONFIG_CHANGE, Holders.config)
I've realized this can be done accessing the mailSender bean from the context and updating it like is explained here
Changing mail configuration in runtime
However if #rmlan solution finally works it may be a much cleaner solution.
Actually thr rmlan solution works with the following fix. Since the onConfigChange compares hashCode of existing config map and new one, so if you set new configs in original configuration (Holders.config.grails.mail), then both configs are same and it never pass the condition to apply new changes, so a new structure should be created and pass it to notifyOfEvent method to mark the change as different hashCodes.
def mailConfig = [ grails: [ mail: [:] ] ]
mailConfig.grails.mail.host = newHost
mailConfig.grails.mail.port = newPort
Holders.pluginManager.getGrailsPlugin('mail').
notifyOfEvent(GrailsPlugin.EVENT_ON_CONFIG_CHANGE, mailConfig)
Still using async-mail and this one make the below exception
No qualifying bean of type [grails.plugin.mail.MailService] is defined: expected single matching bean but found 2: nonAsynchronousMailService,mailService
that is thrown because of the following part of onConfigChange
event.ctx.getBean(MailService.class).setPoolSize(mailConfig.poolSize?:null)
Commenting it let it works as a workaround, but making sendMail of mail plugin is called, not async-mail, so exception may raise if async-mail features is used on constructing mail. Hence to use async-mail in this workaround should use sendAsynchronousMail method.

grizzly logs to stderr, annoying in eclipse

When i run my junit jersey service tests using the grizzly framework in eclipse, the log is directed to stderr. As a result the console window grabs focus, and the log appears red.
I can't figure out the proper configuration steps. From my reading it looks like i need to add the slf4j.jar to my pom.xml and add a logging properties file somewhere? But i'm unsure which slf4j jars to add (there are many) or where to place the logging properties file.
Or, frankly, if this is the right approach in general.
p.s. also i am aware i can turn off the "show console when standard error changes" feature in eclipse, but i'd rather not paint over the problem. :)
It doesn't look to me like Grizzly used slf4j, but rather the "standard" java.util.logging framework. If that's the case, you can read about configuring it here: http://docs.oracle.com/javase/6/docs/technotes/guides/logging/overview.html#1.8
With Eric's help above I created this class:
package org.trebor.www;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Logger;
public class LoggerTrap
{
public LoggerTrap()
{
Handler handler =
new ConsoleHandler()
{
{
setOutputStream(System.out);
}
};
Logger.getLogger("").addHandler(handler);
}
}
and added this jvm arg
-Djava.util.logging.config.class=org.trebor.www.LoggerTrap
and all java.logging goes to STDOUT. In the process I've learned that I don't much like java.logging.