Log info into the default log file of the server - java

My Dev machine has JBoss while production server is running tomcat8 on AWS.
How do I setup Log (java.util.loggin) to log into default log file in the default directory, for instance following are the default paths for log files for each server. (no 3rd party loggers please)
/log/tomcat8/catalina
jboss-4.0.4/server/some_server_conf/log/
In some code examples I've seen, a FileHandler("file.log") is provided but then this log exists locally within the project folder and is not accessible from outside in a production environment. I want the application to be part of the root logging system that appends the log info into default directories and into default files.
Lateral Thinking: Please advise if there's a totally different strategy for production servers.
Part of the reason is that it's easier to see the log files right from AWS by requesting last 100 lines and I'd like to append some additional meaningful information there regarding my application as it runs.
public class Service {
private static Logger logger = Logger.getLogger("myproject");
public Service() {
logger.log(Level.INFO, "abc");
}
}

The easiest solution would be to simply install a java.util.logging.ConsoleHandler on the root logger of your myproject. That would direct all of your project output to the tomcat8-stderr.YYYY-MM-DD.log.
Otherwise you can create a ServletContextListener and install a custom handler on the root logger of your project which formats and logs to the ServletContext.log methods.

Related

OWASP ESAPI and SLF4J/Logback setup

I currently have a program that uses SLF4J/LoggerFactory for capturing logs and the configuration is done through logback.xml. My logs were working as expected. I was recently instructed by the Security team at my job to update a class which uses ESAPI. I updated the class and added ESAPI.properties and validation.properties to src/main/resources/esapi. In ESAPI.properties, I updated ESAPI.Logger to ESAPI.Logger=org.owasp.esapi.logging.slf4j.Slf4JLogFactory. Based on my research online and through SO, I assumed I would be able to us my logs (which is initialized as protected final static Logger log = LoggerFactory.getLogger(this.class); and used as log.info("Example")) as normal (outputs the logs to a file on a server) but the logs just output to the CL when the java program is run.
The configuration for the LogFactory indicates which Logging Framework instance org.owasp.esapi.Logger should delegate to.
From your question, it appears you are expecting a log event to follow the flow of:
MyCode -> SLF4J -> ESAPI -> Console.
In actuality, the way it's implemented is:
MyCode -> ESAPI_LOGGER -> SLF4J -> Console
The ESAPI Logger is not an implementation of the SLF4J logging contract. It will only delegate to the logger instance configured in esapi.properties. All of the references in code will need to be updated to
Logger esapiLogger = ESAPI.getLogger(myLogger_byClass_or_byName)
There are also these relevant properties in the ESAPI.properties file that you may wish to tweak:
# ESAPI Logging
# Set the application name if these logs are combined with other applications
Logger.ApplicationName=ExampleApplication
# If you use an HTML log viewer that does not properly HTML escape log data, you can set LogEncodingRequired to true
Logger.LogEncodingRequired=false
# Determines whether ESAPI should log the application name. This might be clutter in some single-server/single-app environments.
Logger.LogApplicationName=true
# Determines whether ESAPI should log the server IP and port. This might be clutter in some single-server environments.
Logger.LogServerIP=true
# Determines whether ESAPI should log the user info.
Logger.UserInfo=true
# Determines whether ESAPI should log the session id and client IP.
Logger.ClientInfo=true
I resolved my issue after reading the README.md for the current release which references the 2.2.3.0 release notes if you are using the slf4j libraries. I needed to exclude the slf4j-simple which is pulled in because it is a dependency of AntiSamy 1.6.2.

mixed java logging with slf4j, log4j and java.util.logging

I'm running a web application in a Weblogic server (Im not realy familiar with ).
Via JVM args a log4j config is passed with log level DEBUG to the application.
In the log file I can also find some log entries of DEBUG level.
So far so good.
During debugging I found some calls to logger.debug() that are not in the log file.
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ActionCtr.class);
The method call is definitely hit but nothing is written to a file.
If I do a step into during debugging I see in the logger:
org.slf4j.impl.JDK14LoggerAdapter(com.example.application.ActionCtr)
"java.util.logging.FileHandler.pattern" -> "%h/java%u.log"
And this leads to some questions for me (as I can not change the running application):
1) how could it bee that it uses the application is using a mixed up log4j and java.util.logging
2) How could I determine what is used in what classes?
3) There is no %h/java%u.log (~/java*.log) so I've tried to provide a java.util.logging conform properties file,
but this changed nothing - how an I determine where the running logger got its config from to configure it right?
1) how could it be that it uses the application is using a mixed up log4j and java.util.logging
Any of those logging frameworks could be used by the application directly or a dependent library that the application is using. It doesn't take too many dependencies to end up with a bunch of logging framework hitchhikers.
The SLF4J manual explains how that logging framework wrangles all of these other frameworks. This why you are seeing the org.slf4j.impl.JDK14LoggerAdapter.
How could I determine what is used in what classes?
Assuming you mean direct usages you can use Jdeps or Javap.
how an I determine where the running logger got its config from to configure it right?
The JConsole tool can access the JUL loggers at runtime. It will also show you all of the system properties which may include paths to logging.properties files.
If the application is pragmatically configuring the logging in an non-standard way then one option would be to use the java.security.debug using the access option. Run the application under a security manager will all or all required permissions but then enable access tracing.

websphere liberty - application specific logging configuration

I am using SLF4J in my application which is deployed on websphere liberty profile. I want to configure it so that 1- logging configuration (such as log4j properties if I use log4j with slf4j) resides outside the application war file, 2 - The logging configuration applies only to my application and not to other applications deployed on the same profile.
(1) can be achieved by using slf4j with java.util.logging since websphere liberty is integrated with that, however the logging configuration applies to the entire profile
However, what would be the best way to get both (1) and (2) ?
I ended up using the org.apache.log4j.PropertyConfigurator for this along with the java.nio.file.WatchService.
The watchservice will watch a log4j.properties file at a predefined location for changes. If the file is changed it will call this method
/**
* Update configuration.
*/
public void updateConfiguration() {
Path path = configPath.resolve(LOG_FILE_NAME);
if (Files.exists(path)){
System.out.println(MessageFormat.format("Reloading logging configuration from {0}", path.toAbsolutePath()));
PropertyConfigurator.configure(path.toAbsolutePath().toString());
} else {
System.err.println(MessageFormat.format("log4j.properties file was expected at {0} but was not present. Please create this file and configure it for logging to work.", path.toAbsolutePath()));
}
}
This will allow for a log configuration file outside the application which will also be updateable at runtime.

Change logging level in Tomcat at runtime?

I am using JULI logging framework in current Tomcat 7, providing a webapp specific logging.properties in my WEB-INF/classes folder.
Now I am looking for a way to change the log level of a logger at runtime.
I found several sources saying that changing the level of a logger at runtime might be possible via MBean Server. Unfortunately the appropriate MBean "java.util.logging.Logging" does not list my webapp specific logger, so I cannot execute the method "setLoggerLevel".
Does anyone know how to accomplish this? Thanks for any hints - excluding those suggesting to use log4j, that is... ;-)
Connecting JConsole to Tomcat and using the MBean tab to change the logger level is the ideal way. One issue is that the logger names don't exist unless the code has triggered the creation of a logger. You can't use the MBean to create a logger ahead of the code running. You should use JConsole to double check that the MBean itself doesn't exist.
Tomcat installs a custom LogManager that filters by the current class loader. JMX would be using the system class loader so it is possible that ClassLoaderLogManager won't return the logger names because the current classloader is not the web app classloader.
You could always create a servlet/jsp form or webservice deployed with the application to get the logger and set the level. You might have to pin each modified logger in memory to keep your logger level active. You can free the logger once the level is set back to null. You'll have to deal with security concerns with including such a page.
I wouldn't do this on a production server but you can configure a WatchedResource in tomcat that points to your logging.properties. Then any time that file is updated the web app is redeployed with the new settings. Watch out for ClassLoader leaks going that route.

app log file miss most log info when do concurrence testing

i use JMeter to do concurrence testing for web app.
environment : linux , JMeter 2.6, tomcat 6.0.35, log4j 1.2.14,
i use appender of my company which extends RollingFileAppender, it's used for creating corresponding log file according to the different user and login ip, e.g. user1-233.111-app.log.20121112
i set the threads of JMeter is 50, then run the jmeter script to test web app, there are 50 log files in tomcat/logs, but there are next problems :
each log file miss most log info, e.g. user1-233.111-app.log.20121112 only has one line info, but it should has much log info
user1's log file contains user2's log info, e.g. in user1-233.111-app.log.20121112, there are some lines like - INFO [user2] ..........
there is not user1-233.111-app.log.20121112.1, user1-233.111-app.log.20121112.2 etc
Thanks in advance!
This is surely a bug in your file appender extension class. Show code here so that you can get answer .

Categories

Resources