Hybris: How to create separate log4j log files in the same extension - log4j

I need to log some audit logs to a separate file than standard console logs. I tried to create custom logger with the properties added to custom extension
log4j.appender.auditLog=org.apache.log4j.FileAppender
log4j.appender.auditLog.File=/somelogsfile.log
log4j.appender.auditLog.layout=org.apache.log4j.PatternLayout
log4j.appender.auditLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n
log4j.category.auditLogger=INFO, auditLog
log4j.additivity.auditLogger=false
Then i have initialised logger as:
Logger LOG2= Logger.getLogger("auditLogger");
LOG2.info("Test");
The logs are output to standard console logs but not to the specified File. What am I missing?

Based on the updated question, I did some extra investigation and tested it out on my own system.
Most likely, your hybris version is making use of log4j2 this means that the config should be like this:
log4j2.appender.auditLog.type=File
log4j2.appender.auditLog.name=auditLog
log4j2.appender.auditLog.fileName=${HYBRIS_LOG_DIR}/audit.log
log4j2.appender.auditLog.layout.type=PatternLayout
log4j2.appender.auditLog.layout.pattern=%d [%24F:%t:%L] - %m%n
log4j2.logger.auditLogger.name=auditLogger
log4j2.logger.auditLogger.level=info
log4j2.logger.auditLogger.appenderRefs=auditLogger
log4j2.logger.auditLogger.appenderRef.auditLogger.ref=auditLog
log4j2.logger.auditLogger.additivity=false
I've tested this out on my local system, and using Logger LOG= Logger.getLogger("auditLogger") prints the log statements to the new file

Related

How to enable /disable the RequestLogger in console?

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.

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);

how to change the logging level of logger in the code overriding value set on config

I have a C# Win Form application and in each class i get the logger like this:
private static readonly ILog Log = LogManager.GetLogger("ApplnLogger");
I configured in my app.config to log messages at info level and above.
Now when a issue is reported i would like to set the logger level to debug so that i will have information in log file necessary for debugging the issue.
How do I set the logger level to debug overriding the config level of INFOwithout having to restart my win form application?
Check out ConfigureAndWatch
Basically, you tell your program to watch the configuration file for changes. So, if you edit this file, Log4net will automatically use the new settings without you having to restart your application.

Resources