Here's the code in my main method:
public static void main(String argv[]) {
log1.setLevel(org.apache.log4j.Level.TRACE);
System.out.println("Log level set in Class1 is " + log1.getLevel());
log2.setLevel(org.apache.log4j.Level.TRACE);
System.out.println("Log level set in Class2 is " + log2.getLevel());
Class1.doLog();
Class2.doLog();
}
Here are the log statements in my classes:
public static class Class1 {
public static org.apache.log4j.Logger log1 = org.apache.log4j.Logger.getLogger(NewClass1.class);
static void doLog() {
System.out.println("Log level in Class1.doLog() is " + log1.getLevel());
log1.trace("Trace Message! - Class1.doLog()");
log1.debug("Debug Message! - Class1.doLog()");
log1.info("Info Message! - Class1.doLog()");
log1.warn("Warn Message! - Class1.doLog()");
log1.error("Error Message! - Class1.doLog()");
log1.fatal("Fatal Message! - Class1.doLog()");
}
}
I tried to create a logger inside a custom class; I created logger statements inside the class's method as well. My Netbeans compiler displayed an error message for log commands in main: the symbol cannot be found.
I followed Netbeans' suggestions by creating my own log classes and methods; the console outputs aren't displayed properly in the console (ie, missing line numbers, changed log level isn't read properly, etc.) – they don't recognize the log commands from the imported Logger library.
The error I can see over here is that you are creating the log1 and log2 both the object inside the class Class1 and Class2 respectively which is not accessible to main method. Both the log variables you created are local to those class that you have created. Try declaring both the objects just after the main class that you have declared.
So after making the changes code will look like this:
class Main {
public static org.apache.log4j.Logger log1 = org.apache.log4j.Logger.getLogger(NewClass1.class);
public static org.apache.log4j.Logger log1 = org.apache.log4j.Logger.getLogger(NewClass1.class);
public static class Class1 {
log1 = org.apache.log4j.Logger.getLogger(Class1.class);
static void doLog() {
System.out.println("Log level in Class1.doLog() is " + log1.getLevel());
log1.trace("Trace Message! - Class1.doLog()");
log1.debug("Debug Message! - Class1.doLog()");
log1.info("Info Message! - Class1.doLog()");
log1.warn("Warn Message! - Class1.doLog()");
log1.error("Error Message! - Class1.doLog()");
log1.fatal("Fatal Message! - Class1.doLog()");
}
}
public static class Class2 {
log2 = org.apache.log4j.Logger.getLogger(Class2.class);
static void doLog() {
System.out.println("Log level in Class1.doLog() is " + log1.getLevel());
log1.trace("Trace Message! - Class1.doLog()");
log1.debug("Debug Message! - Class1.doLog()");
log1.info("Info Message! - Class1.doLog()");
log1.warn("Warn Message! - Class1.doLog()");
log1.error("Error Message! - Class1.doLog()");
log1.fatal("Fatal Message! - Class1.doLog()");
}
}
public static void main(String argv[]) {
log1.setLevel(org.apache.log4j.Level.TRACE);
System.out.println("Log level set in Class1 is " + log1.getLevel());
log2.setLevel(org.apache.log4j.Level.TRACE);
System.out.println("Log level set in Class2 is " + log2.getLevel());
Class1.doLog();
Class2.doLog();
}
}
This should work fine to use the log objects inside main method.
Another Method
If you don't want to declare the variables globally and want to access the log objects. Since you have declared the variables as static you can directly call them using class name.
So main method looks like this:
public static void main(String argv[]) {
Class1.log1.setLevel(org.apache.log4j.Level.TRACE);
System.out.println("Log level set in Class1 is " + log1.getLevel());
Class2.log2.setLevel(org.apache.log4j.Level.TRACE);
System.out.println("Log level set in Class2 is " + log2.getLevel());
Class1.doLog();
Class2.doLog();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.