Tolgar
asked on
How to fix Java warning for log4j ?
Hi,
When I compile my java code I get the following warning:
I use log4j to log the errors. How can I get rid of this error? Or actually, how can I fix it?
Thanks,
When I compile my java code I get the following warning:
log4j:WARN No appenders could be found for logger (org.apache.axis.description.OperationDesc).
log4j:WARN Please initialize the log4j system properly.
I use log4j to log the errors. How can I get rid of this error? Or actually, how can I fix it?
Thanks,
put log4j.properties on your classpath
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@dravidnsr: So what should i change in the log4j.properties file?
@garypfirstech: Here is my log4j.properties file. I use Maven to compile my code. Still getting the same issue. I tried to put this file in the same directory where my code is. It did not work. Then I put it under resources directory and it did not work again. And if I need to put it in the classpath, how can I do it?
Thanks,
@garypfirstech: Here is my log4j.properties file. I use Maven to compile my code. Still getting the same issue. I tried to put this file in the same directory where my code is. It did not work. Then I put it under resources directory and it did not work again. And if I need to put it in the classpath, how can I do it?
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# An example log4j configuration file that outputs both to System.out
# and a file named 'test'.
# For the general syntax of property based configuration files see the
# documenation of org.apache.log4j.PropertyConfigurator.
# WARNING: Location information can be useful but is very costly in
# terms of computation.
# The root logger uses the appender called A1.
# The root logger uses the appenders called A1 and A2. Since no level
# is specified, note the empty string between the comma (",") and the
# equals sign ("="), the level of the root logger remains
# untouched. Log4j always initializes the level for the root logger to
# DEBUG. The root logger is the only logger that has a default
# level. Bu default, all other loggers do not have an assigned level,
# such that they inherit their level instead.
log4j.rootLogger=, A1, A2
# A1 is set to be ConsoleAppender sending its output to System.out
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
# The conversion pattern consists of date in ISO8601 format, level,
# thread name, logger name truncated to its rightmost two components
# and left justified to 17 characters, location information consisting
# of file name (padded to 13 characters) and line number, nested
# diagnostic context, the and the application supplied message
log4j.appender.A1.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n
# Appender A2 writes to the file "errorLog".
log4j.appender.A2=org.apache.log4j.FileAppender
log4j.appender.A2.File=errorLog
# Truncate 'test' if it aleady exists.
log4j.appender.A2.Append=false
# Appender A2 uses the PatternLayout.
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%-5r %-5p [%t] %c{2} - %m%n
# In this example, we are not interested in INNER loop or SWAP
# messages. You might try to set INNER and SWAP to DEBUG for more
# verbose output.
log4j.logger.org.apache.log4j.examples.SortAlgo.INNER=INFO
log4j.logger.org.apache.log4j.examples.SortAlgo.SWAP=INFO
Thanks,
Try setting the log4.debug system property (-Dlog4j.debug) and also set the log4j.configuration system property to the absolute pathname of your log4j.properties file.
>>>>>change this
log4j.rootLogger=, A1, A2
into
log4j.rootLogger= A1, A2
remove the , infront of A1
log4j.rootLogger=, A1, A2
into
log4j.rootLogger= A1, A2
remove the , infront of A1
David - This will cause log4j to interpret A1 as a debugging level, not an appender name and will cause problems. The initial comma indicates that the debugging level is omitted so it defaults to DEBUG.
ASKER
ok. I managed to make it work. But it puts everthing in the log file. I only want the warnings and error messages.
So I did this:
But when I compile is says:
How can I fix it?
Thanks,
So I did this:
static final Logger logger = Logger.getLogger(batLogData2gecko.class);
logger.setLevel(Level.WARN);
But when I compile is says:
myFile.java:[30,16] <identifier> expected
How can I fix it?
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ok .I made this change.
Normally if no warnings or errors occur, is this errorLog file created or is it only created when an error or warning occurs?
Because, when I run my code after changing it to warn no errorLog file was generated.
Thanks,
Normally if no warnings or errors occur, is this errorLog file created or is it only created when an error or warning occurs?
Because, when I run my code after changing it to warn no errorLog file was generated.
Thanks,
I believe that it's created on the first write of a log message to it.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Well, then I changed this line:
to
Thanks,
log4j.appender.A2.File=errorLog
to
log4j.appender.A2.File=/path/To/File/errorLog
Thanks,
At launch, define the system property log4j.configuration (using the -D option on the command line) to point to your configuration file. If you don't specify the system property, log4j will look for a file called log4j.properties on your classpath. It couldn't find such a file so it sent you the message that you received.
Look at the small sample configuration files in the "Configuration" section and post here if you have additional questions.