I am using Geotools (version 19.2) to fetch some features. To deactivate the Geotools logging (< SEVERE) I tried 2 things:
As I understood the logging documentation (documentation page):
Logging.getLogger("org.geotools").setLevel(Level.SEVERE);
Loading a custom logging.properties configuration
The configuration (logging.properties) looks like this:
# standard log level
.level = WARNING
handlers = java.util.logging.ConsoleHandler
## limit the messages that are printed on the console to >= WARNING!
java.util.logging.ConsoleHandler.level = WARNING
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.encoding = Cp850
# do not use logging files!
java.util.logging.FileHandler.level = OFF
## just output geotools logs with level SEVERE!
org.geotools.level = SEVERE
Then I am loading the configuration using this code:
LogManager.getLogManager().readConfiguration(MyMainClass.class.getClassLoader().getResourceAsStream("logging.properties"));
Using both approaches I get NO logging output if I run my program in Eclipse. If I run my program as a JAR-file I get the following unwanted logging output:
Nov. 08, 2018 9:48:13 VORM. org.geotools.jdbc.JDBCDataStore
getAggregateExpression INFO: Visitor class
org.geotools.feature.visitor.CountVisitor has no aggregate attribute.
The INFO log resides from private Expression getAggregateExpression(FeatureVisitor visitor) in JDBCDataStore (see GitHub)
Any ideas why the logging configuration does not work for the generated JAR?
Using the JConsole (following answer on Suppress java util logging from 3rd party jar) I figured out that my logging configuration was overwritten by my Java installation's logging.properties file.
I tried to change the logging configuration in my main method which was wrong.
Initializing my custom logging.properties in a separate class and specifying the needed system property solves the problem!
Custom initialization class
public class CustomLoggingPropertiesLoader {
// Initialize the global LogManager
public CustomLoggingPropertiesLoader() {
try {
LogManager.getLogManager().readConfiguration(CustomLoggingPropertiesLoader.class.getClassLoader().getResourceAsStream("logging.properties"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Use system property ("java.util.logging.config.class")
java
-Djava.util.logging.config.class=de.example.logging.CustomLoggingPropertiesLoader
-classpath [myclasspath]
Related
I'm trying to do some Logging modification on My Standard AppEngine Java 11 app using a logging.properties file, My app is a Jetty web-server which I run by adding the following entry-point to my app.yaml file
runtime: java11
entrypoint: 'java -cp "*" com.jettymain.Main webapp.war'
Now Google documentation here suggests that in order to use logging.properties. The path to the configuration file must be provided to your application as a system property: -Djava.util.logging.config.file=/path/to/logging.properties
I have tried setting that in the code, first thing in the com.jettymain.Main class which starts the Jetty embedded web-server by doing the following
System.setProperty("java.util.logging.config.file", "WEB-INF/logging.properties")
But that didn't work, modifying the entry-point in app.yaml did make the web-server load those configurations but it was failing to load the Google cloud logging handler class com.google.cloud.logging.LoggingHandler, which I need to write those logs to Google Stackdriver, I have the Google cloud logging dependency added to both my app and the web-server but that didn't help.
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud-logging</artifactId>
<version>3.0.1</version>
</dependency>
modified entry-pint
runtime: java11
entrypoint: 'java -cp "*" com.jettymain.Main webapp.war -Djava.util.logging.config.file=WEB-INF/logging.properties'
logging.properties file, it is the sample file which can be found here plus few extra things
# To use this configuration, add to system properties : -Djava.util.logging.config.file="/path/to/file"
.level = INFO
# it is recommended that io.grpc and sun.net logging level is kept at INFO level,
# as both these packages are used by Cloud internals and can result in verbose / initialization problems.
io.grpc.netty.level=INFO
sun.net.level=INFO
handlers=com.google.cloud.logging.LoggingHandler
# default : java.log
com.google.cloud.logging.LoggingHandler.log=custom_log
# default : INFO
com.google.cloud.logging.LoggingHandler.level=FINE
# default : ERROR
com.google.cloud.logging.LoggingHandler.flushLevel=EMERGENCY
# default : auto-detected, fallback "global"
com.google.cloud.logging.LoggingHandler.resourceType=container
# custom formatter
com.google.cloud.logging.LoggingHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%3$s: %5$s%6$s
# Level for all logs coming from GWT client (can't filter by specific classes on client)
com.google.gwt.logging.server.RemoteLoggingServiceUtil.level = WARNING
com.beoui.geocell.level=WARNING
It might be an old question, but this answer still might help to someone having same issue.
I've managed to add custom logging.properties using Gradle for Java 11 Standard Environment:
First of all, you have to add dependency to the Cloud logging as it's no longer provided by the environment:
dependencies {
runtimeOnly("com.google.cloud:google-cloud-logging:3.11.3")
// your other dependencies
}
Then you need to specify a folder containing your logging.properties file to be uploaded by Gradle com.google.cloud.tools.appengine plugin:
configure<AppEngineAppYamlExtension> {
stage {
setArtifact("build/libs/server.jar")
// can be some other folder of your choice where logging.properties is located
setExtraFilesDirectories("src/main/resources")
}
deploy {
version = "GCLOUD_CONFIG"
projectId = "GCLOUD_CONFIG"
}
}
Then specify an entrypoint in app.yaml. The trick here is that properties file can be found in /workspace/ directory of the GAE container:
runtime: java11
entrypoint: 'java -jar -Djava.util.logging.config.file=/workspace/logging.properties server.jar'
Deploy application using
./gradlew appengineDeploy
My logging.properties file:
.level = INFO
# it is recommended that io.grpc and sun.net logging level is kept at INFO level,
# as both these packages are used by Cloud internals and can result in verbose / initialization problems.
io.grpc.netty.level=INFO
sun.net.level=INFO
handlers=com.google.cloud.logging.LoggingHandler
com.google.cloud.logging.LoggingHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.SimpleFormatter.format=%3$s: %5$s%6$s
Get the System.setProperty into any method. It can be the main method also. I think the problem is "the logging.properties must go in WEB-INF/classes directory" as per this link and not WEB-INF/ directory as mentioned in your code.
System.setProperty("java.util.logging.config.file", "WEB-INF/classes/logging.properties")
When developing a simple plugin I have code like this:
import com.intellij.openapi.diagnostic.Logger;
public class MyClass {
private static final Logger LOG = Logger.getInstance(MyClass .class.getName());
public MyClass(){
LOG.warn("Creating class warn");
LOG.info("Creating class info");
}
}
I see in this thread https://intellij-support.jetbrains.com/hc/en-us/community/posts/206779715-Proper-way-to-log-in-Idea-plugins
Using this API is the recommended way of logging...
However, in the Console output, whe I run the plugin in the sandbox IDE, I only see the WARN level output. (I am using GrepConsole, but i checked and am not squelching any INFO levels).
I also manually checked the file in sandbox/system/logs/idea.log and the INFO statements are there.... they are just not getting to my IDE console.
Is there a way I can configure my project to allow INFO level output using this logging class?
also manually checked the file in sandbox/system/logs/idea.log and the INFO statements are there.... they are just not getting to my IDE console.
This is expected. The console prints the standard error/output stream. And the logger writes logs into the idea.log file.
I'm using java system logging in tomcat 7, but no logging statements get written to the log. I've added this file to my WEB-INF/classes. The log file "new-xyz-test" gets created (so I have at least some of the config right) but its empty - no log statements get printed to it.
handlers=java.util.logging.ConsoleHandler, org.apache.juli.FileHandler
org.apache.juli.FileHandler.level=ALL
org.apache.juli.FileHandler.directory=${catalina.base}/logs
org.apache.juli.FileHandler.prefix=new-xyz-test-
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
com.xyz.level=ALL
com.xyz.handlers=org.apache.juli.FileHandler
To configure JULI in the web applications you need have a logging.properties file in the WEB-INF/classes directory. If you use the default handlers, you may lose messages. You need to specify a prefix for the handler in your file.
handlers=1FILE.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler
.handlers=java.util.logging.ConsoleHandler
1FILE.org.apache.juli.FileHandler.level=FINEST
1FILE.org.apache.juli.FileHandler.directory=/app-logs
1FILE.org.apache.juli.FileHandler.prefix=file-1
java.util.logging.ConsoleHandler.level=FINE
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
com.xyz.level=INFO
com.xyz.handlers=1FILE.org.apache.juli.FileHandler
com.abc.level=INFO
com.abc.handlers=java.util.logging.ConsoleHandler
A
handler prefix (e.g. 1FILE.) starts with a number, then has an arbitrary string, and ends with a period (.).
See more in Logging in Tomcat
Arguments in the JVM
If you are not running the Tomcat from the startup.sh or startup.bat, you need to specify:
The location of the general logging.properties for Tomcat (in the conf directory of Tomcat)
The manager org.apache.juli.ClassLoaderLogManager. This is important because allows you to configure
for each web application different loggin options. By default, a JVM process can only have a single configuration file.) ,
Similar to the next (I'm using eclipse):
-Djava.util.logging.config.file="C:\Users\Paul\workspaces\utils\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\logging.properties" -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
By default, java.util.logging read the file that is included in the JDK or JRE, e.g.:
"C:\Software\jdk1.7.0_17\jre\lib\logging.properties"
Setting Tomcat Heap Size (JVM Heap) in Eclipse, for how to add arguments in the VM
are you sure that you write to the correct logger , i.e. Logger.getLogger("com.xyz")?
I think that you may got wrong when you wrote in logging.properties:com.xyz.level=ALL com.xyz.handlers=org.apache.juli.FileHandler in the case that you actually write to the logger Logger.getLogger(com.xyz.YourClass.class), that because in the logging properties file you should write the logger name which is in your case com.xyz.YourClass
This is similar to this other question, although I already put the logging.properties in the executable jar and doesn't work.
I have a class (ReportGenerator) that has the following:
Logger logger = Logger.getLogger(ReportGenerator.class.getName());
logger.log(Level.INFO, "LOG THIS");
I'm using Netbeans so I put the logging.properties file in the path src/main/resources. It has this (among other things):
# default file output is in user's home directory.
java.util.logging.FileHandler.pattern = /my/folder/reports.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 10
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
# Limit the message that are printed on the console to INFO and above.
java.util.logging.ConsoleHandler.level = OFF
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################
# For example, set the com.xyz.foo logger to only log SEVERE
# messages:
com.mypackage.ReportGenerator.level = ALL
The jar is generated using Maven, when decompressed I can see that the logging.properties is in the main folder of the jar. Along with the folder com where my class is.
-com
-mypackage
-ReportGenerator
logging.properties
...other things
When I run from console:
java - jar MyReportsJar.jar
It shows me the logs through the console. I want to log it to the file I set in the logging.properties.
What am I doing wrong?
How do I do it without setting the JVM java.util.logging.config.file param?
After dealing a few days against this and reading many resources on the Internet I came up with this solution:
I have to change the logging properties of the LogManager in the program. I can use the logging.properties file packaged in the .jar like this:
LogManager.getLogManager().readConfiguration(ReportGenerator.class.getClassLoader().getResourceAsStream("logging.properties"));
And then I can do the same as before to get the logger and log:
Logger logger = Logger.getLogger(ReportGenerator.class.getName());
logger.log(Level.INFO, "LOG THIS");
But I found very useful that you can specify another logging.properties file on runtime so my final code is this:
String logFile = System.getProperty("java.util.logging.config.file");
if(logFile == null){
LogManager.getLogManager().readConfiguration(ReportGenerator.class.getClassLoader().getResourceAsStream("logging.properties"));
}
Logger logger = Logger.getLogger(ReportGenerator.class.getName());
logger.log(Level.INFO, "LOG THIS");
That way, if I execute the jar as this:
java -jar MyReportsJar.jar
It uses the internal logging.properties file.
But if I execute:
java -Djava.util.logging.config.file=<external-logging.properties> -jar MyReportsJar.jar
it uses the external logging.properties file.
I'm a newbie to log4j. This is what I have . I have about 20 files in different packages in a STAND ALONE JAVA APPLICATION.
I am trying to use and write log files.
Following is my log4j.properties file which is in my class path:
log4j.appender.R = org.apache.log4j.DailyRollingFileAppender
log4j.appender.R.File = /ParentFolder/ChildFolder/application.log
log4j.appender.R.Append = true
log4j.appender.R.DatePattern = '.'yyy-MM-dd
log4j.appender.R.layout = org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} %c{1} [%p] %m%n
Following is the code to initialize logging in my main method
final String LOG_FILE = "C:/eclipse_workspace/lib/log4j.properties";
Properties logProp = new Properties();
try
{
logProp.load(new FileInputStream (LOG_FILE));
PropertyConfigurator.configure(logProperties);
logger.info("Logging enabled");
}
catch(IOException e)
{
System.out.println("Logging not enabled");
}
In every java class of the application I have the following code
import org.apache.log4j.*;
private static final Logger logger = Logger.getLogger(TheActualClassName.class);
But I get the following warning messages when I run the app.
log4j:WARN No appenders could be found for logger (com.xxx.myApp.MainProgram.MyFileName).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
What am I doing wrong?? The log file "application.log" is not being generated
May need the following line:
# Set root logger level to INFO and appender to R.
log4j.rootLogger=INFO, R
The root logger is always available and does not have a name.
Since the version 1.2.7, log4j (with the LogManager class) looks for log4j.xml in the classpath first. If the log4j.xml not exists, then log4j (with the LogManager class) looks for log4j.properties in the classpath.
Default Initialization Procedure
LogManager xref
If you are going to use a file named log4j.properties, and it's on your application's classpath, there is no need to even call PropertyConfiguration or DOMConfigurator - log4j will do this automatically when it is first initialized (when you first load a logger).
The error message seems to indicate that your configuration is not being loaded.
Add the VM argument -Dlog4j.debug to your application to have log4j spit out a whole bunch of information when it starts up, which includes which files it tries to load and what values it finds in the configuration.
Raghu ,if you are using stand alone configuration for configuring log4j Properties then use can use BasicConfigurator.configure() method for solving your appenders issue.