How to enable /disable the RequestLogger in console? - log4j

Want to print API request in log with the help of RequestLogger. We have log4j properties file ,log4j dependency entry is there in pom and also POM is referring the log4j properties file too.
We have log4j properties under resources and also added dependency in POM file . And tried to dd
RequestLogger requestLogger = new RequestLogger(NullPrintStream.NULL_PRINT_STREAM);
TestBaseProvider.instance().get().getContext().setProperty("rest.client.requestlogger", requestLogger);
the above lines in OnStart listener method. But nothing works fine, Can anyone please guide how we can print api requests in log.

Looking at the source code of RequestLogger, it can operate in two ways: by sending messages to Jakarta Commons Logging or to a PrintStream, depending on the constructor you use:
by calling new RequestLogger(NullPrintStream.NULL_PRINT_STREAM) you send everything to a PrintStream, which:
Writes all data to the famous /dev/null.
This print stream has no destination (file/socket etc.) and all bytes written to it are ignored and lost.
by calling new RequestLogger(), you send everything to JCL, which is a logging API with a configurable backend. If you use Log4j2 and have log4j-jcl on the classpath, JCL will choose Log4j2. If you use the (EOL-ed) Log4j 1.2 and have no other JCL binding on the classpath, JCL will choose this one.

Related

Mule not honouring log4j2.component.properties or log4j2.system.properties

I've a mule application which needs to load log4j2.xml from different locations as per the environment shown below.
app1
dev --> /etc/dev/app1/log4j2.xml
sit --> /etc/sit/app1/log4j2.xml
. . .
prod --> /etc/prod/app1/log4j2.xml
I don't want to use spring bean loading as by the time this bean is loaded, Mule would have already initiated log context for this app1 with default configuration and writes few logs to it.
Within log4j functionality, there are log4j2.system.properties and log4j2.component.properties files. When either of them is added to classpath (src/main/resources) with log4j.configurationFile property in it, it is supposed to pick up this file during application startup itself.
Reference: Log4j System Properties
log4j.configurationFile=${config.path}/app1/log4j2.xml
config.path is defined in wrapper as system property and available to app1 holding the env path ("/etc/dev" if dev or "/etc/sit" if sit etc..)
However, both of these files are not picking up by Mule and resolving to default configuration.
Can someone please assist in making any of these files pick up by Mule during application startup itself?
After long research, we have to update mule_artifact.json with "logConfig" key to define the location of external log4j2.xml file in server relative to mule_home path.
The same path may not work in local but you can create "mklink" to resemble server path in local.
I've tested successfully both.

Logging with Nlog & Rebus

I would like to implement ReBus logging with Nlog.
We have NLog running to console, file and Database (with custom fields) and different files. I mean, NLog is up & running fine.
We have installed ReBus.Nlog Nuget Package and changed our adapter configuration to Nlog().
But nothing is logged to files in spite of we have it configured with 'Trace' loglevel.
Somebody has any simple example to implement loggin with NLog in ReBus?
I have checked the tests on Rebus.Nlog github source code, but I think it is only testing explicit sent messages to logger.
Mainly I would like to log Rebus warnings and errors.
Thanks.
I have added this Logging sample project to the RebusSamples repository – the relevant part of the code is shown here:
// configure NLog
var configuration = new LoggingConfiguration
{
LoggingRules = { new LoggingRule("*", LogLevel.Debug, new ConsoleTarget("console")) }
};
LogManager.Configuration = configuration;
// configure Rebus
Configure.With(activator)
.Logging(l => l.NLog())
.Transport(t => t.UseInMemoryTransport(new InMemNetwork(), "logging"))
.Start();
As far as I can tell, this way of configuring NLog may not be the idiomatic way, since I guess most people prefer to be able to configure rules and targets with XML in their application configuration files.
Either way, NLog ends up having the static configuration applied, and then Rebus can pick that up when you call NLog() on its logging configurer.
I hope that helps :)

ServiceStack Log4NetFactory

How can I configure log4net in code when I like to use the servicestack logging interface? I see there is
LogManager.LogFactory = new Log4NetFactory(configureLog4Net:true);
If I got things right this expects a XML file containing the Log4Net configuration. I avoid XML config files whenever possible and usually configure Log4Net with my own static LoggerConfig class. There I setup all my appenders (EventLog, File, Console, etc.) using the Log4Net API.
Is there any way to integrate my class with the ServiceStack logging interface?

Logging from within WSO2 custom mediator

I want to be able to log from within a custom mediator that I have built.
A few questions:
What do I need to add to the esb's log4.properties to enable a custom class the ability to write to log files?
From within the custom mediator class, do I need declare the following to log to the synapse log file?
private static final Log log = LogFactory.getLog(<ClassName>.class);
I have seen many examples of using the log mediator, but I need to be able to control more of what I log from within mediator class.
By default your custom mediator logs will be sent to Carbon Log file as well as Carbon Memory and the console. And also you do not need to define
private static final Log log = LogFactory.getLog(.class);
again in your class, you can simply use the log object which is coming from AbstractMediator
Please refer the following guide which explains how to write your custom mediator as well as how you should log inside a mediator.
You can change the level of the log by editing the log4j.properties file or by going to configure -> logging using the management console to get more control on what to log and what not to log.
You can add the mediator class to log4j.properties
log4j.logger.org.foo.bar=ERROR, CARBON_LOGFILE, CARBON_MEMORY
Regards,
/Nuwan

Can multiple log4j.properties files be used in the same Tomcat web app?

I'm writing a custom extension to an off-the-shelf Java web application. The application uses log4j for logging and I'd like to add a new logger and appender specifically for my extension. The problem is that the application manages the log4j.properties file which is dynamically generated based on selections in an admin screen UI. Since this is an "off-the-shelf" application, I can't modify the source code. So, if I add my own logger & appender(s) to the file, it gets overwritten anytime an admin changes logging preferences in the UI.
Is it possible to get log4j to get it's configuration from 2 files? For example, I'd want something like the following:
applog.properties #(Dynamically generated from admin UI)
mylog.properties #(My static properties)
In this scenario, log4j would somehow combine the entries from both files for the complete configuration.
Is this possible? or are there other workarounds?
I never did find a way to "merge" multiple log4j.properties file, but I did find a workable solution. log4j configuration can be manipulated programatically at runtime similar to the code snippet below. This effectively merged my custom log4j settings into the configuration defined by the log4j.properties file, which in my case I couldn't edit.
// Init custom logging
// Define layout
PatternLayout layout = new PatternLayout();
layout.setConversionPattern("%d [%-5p] -- %m%n");
// Create appender
RollingFileAppender appender = new RollingFileAppender();
appender.setFile(LOG_PATH);
appender.setMaxFileSize("2MB");
appender.setMaxBackupIndex(0);
appender.setLayout(layout);
appender.activateOptions(); // It didn't work without this
// Get our logger and add appender.
log = Logger.getLogger("[MyCustomLogger]");
log.setLevel(YOUR_LOGGING_LEVEL_HERE);
log.addAppender(appender);

Resources