Reputation: 105
It's a school homework: I have to build a custom logger class capable of logging from anywhere in my projects.
I cannot use anything else but java.util.logger. I'm using a properties file located in a /conf folder in my project.
I get this error:
Bad level value for property: java.util.logging.FileHandler.level
Can't set level for java.util.logging.FileHandler
I think that's why my debug() method can't print "ddd" in my file and also why can't I format file with this string i set:"[%1$tc] - [%2$s] [%4$s] [%5$s] %n".
I can't get what mistake I could have made in conf file or in code: imports are correct, I have no errors/warnings on code.
I know that I should never user absolute path but I don't think this is my problem, path will be fixed after I resolve this issue.
This output of my try.log in my /test folder:
03-Jan-2018 14:01:03 logger.classLogger warning
WARNING: attention please
please 03-Jan-2018 14:01:03 logger.classLogger error
SEVERE: error
03-Jan-2018 14:01:03 logger.classLogger info
INFO: info
This is my .properties file:
.level=ALL
handlers = java.util.logging.FileHandler
java.util.logging.FileHandler.level = FINE
java.util.logging.FileHandler.append = true
java.util.logging.FileHandler.limit = 1000000
java.util.logging.FileHandler.count = 100
java.util.logging.FileHandler.pattern = %t/Log%u%g.log
java.util.logging.SimpleFormatter.format = "[%1$tc] - [%2$s] [%4$s] [%5$s] %n"
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
This is my class:
public class classLogger {
Logger logger;
FileHandler fh;
SimpleFormatter sf;
public classLogger(String classname){
this.logger = Logger.getLogger(classname);
logger.setLevel(Level.ALL);
try {
String path = "D:\\myprojects\\logger\\conf\\logging.properties";
FileInputStream configFile = new FileInputStream(path);
LogManager.getLogManager().readConfiguration(configFile);
}
catch (IOException ex)
{
}
try {
String directory = "C:\\Documents and Settings\\E520user\\Desktop\\test";
String filename = "\\try.log";
this.fh = new FileHandler(directory+filename);
}
catch (IOException ex1) {
}
this.sf = new SimpleFormatter();
//set formatter
fh.setFormatter(sf);
//add handler
logger.addHandler(fh);
}
public void error(String msg){
logger.severe(msg);
}
public void warning(String msg){
logger.warning(msg);
}
public void info(String msg){
logger.info(msg);
}
public void debug(String msg){
logger.fine(msg);
}
public static void main(String[] args) {
IRSLogger log = new classLogger(classLogger.class.getName());
log.warning("attention please");
log.error("error");
log.debug("dddd");
log.info("info");
}
}
Upvotes: 5
Views: 7326
Reputation: 9756
Tried it out on my Mac, and it works fine
$ cat try.log
"[Wed Jan 03 14:35:22 GMT 2018] - [classLogger warning] [WARNING] [attention please]
""[Wed Jan 03 14:35:22 GMT 2018] - [classLogger error] [SEVERE] [error]
""[Wed Jan 03 14:35:22 GMT 2018] - [classLogger debug] [FINE] [dddd]
""[Wed Jan 03 14:35:22 GMT 2018] - [classLogger info] [INFO] [info]
I however also get this message: Can't set level for java.util.logging.FileHandler
can you try and remove java.util.logging.FileHandler.level = FINE
from your properties file?
TIP
You should remove the "
form this line: java.util.logging.SimpleFormatter.format = "[%1$tc] - [%2$s] [%4$s] [%5$s] %n"
EDIT: This works on my computer
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.*;
public class ClassLogger {
private Logger logger;
private FileHandler fh;
public ClassLogger(String classname){
this.logger = Logger.getLogger(classname);
try {
String path = "/Users/myUser/Workspace/stackoverflow/test/src/main/java/logging.properties";
FileInputStream configFile = new FileInputStream(path);
LogManager.getLogManager().readConfiguration(configFile);
} catch (IOException ex) {
ex.printStackTrace();
}
try {
String directory = "/Users/myUser/Workspace/stackoverflow/test/src/main/java/";
String filename = "try.log";
this.fh = new FileHandler(directory+filename);
} catch (IOException ex1) {
ex1.printStackTrace();
}
//add handler
logger.addHandler(fh);
}
public void error(String msg) { logger.severe(msg);}
public void warning(String msg) { logger.warning(msg); }
public void info(String msg) { logger.info(msg); }
public void debug(String msg) { logger.fine(msg); }
public static void main(String[] args) {
ClassLogger log = new ClassLogger(ClassLogger.class.getName());
log.warning("attention please");
log.error("error");
log.debug("dddd");
log.info("info");
}
}
logging.properties
.level=ALL
handlers = java.util.logging.FileHandler
java.util.logging.FileHandler.append = true
java.util.logging.FileHandler.limit = 1000000
java.util.logging.FileHandler.count = 100
java.util.logging.FileHandler.pattern = %t/Log%u%g.log
java.util.logging.SimpleFormatter.format = [%1$tc] - [%2$s] [%4$s] [%5$s] %n
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
It outputs in try.log
[Wed Jan 03 15:27:55 GMT 2018] - [ClassLogger warning] [WARNING] [attention please]
[Wed Jan 03 15:27:55 GMT 2018] - [ClassLogger error] [SEVERE] [error]
[Wed Jan 03 15:27:55 GMT 2018] - [ClassLogger debug] [FINE] [dddd]
[Wed Jan 03 15:27:55 GMT 2018] - [ClassLogger info] [INFO] [info]
Upvotes: 2