use of java.util.logging.FileHandler in project malmo by Microsoft.
the class TCPSocketHelper method setLogging.
static void setLogging(boolean log) {
logging = log;
if (log == true && filehandler == null) {
try {
filehandler = new FileHandler("TCPLog.txt");
filehandler.setFormatter(new SimpleFormatter());
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
logger.addHandler(filehandler);
}
}
use of java.util.logging.FileHandler in project OpenGrok by OpenGrok.
the class LoggerUtil method initLogger.
public static String initLogger(String logpath, Level filelevel, Level consolelevel) throws IOException {
if (logpath != null) {
File jlp = new File(logpath);
if (!jlp.exists() && !jlp.mkdirs()) {
throw new RuntimeException("could not make logpath: " + jlp.getAbsolutePath());
}
if (!jlp.canWrite() && !Level.OFF.equals(filelevel)) {
throw new IOException("logpath not writeable " + jlp.getAbsolutePath());
}
}
Logger.getGlobal().setLevel(Level.OFF);
getBaseLogger().setLevel(Level.ALL);
StringBuilder logfile = new StringBuilder();
logfile.append(logpath == null ? "%t" : logpath);
logfile.append(File.separatorChar).append("opengrok%g.%u.log");
try {
FileHandler fh = new FileHandler(logfile.toString(), loggerIntProperty("java.util.logging.FileHandler.limit", DEFAULT_FILEHANDLER_LIMIT), loggerIntProperty("java.util.logging.FileHandler.count", DEFAULT_FILEHANDLER_COUNT));
fh.setLevel(filelevel);
String formatter = LogManager.getLogManager().getProperty("java.util.logging.FileHandler.formatter");
try {
fh.setFormatter((Formatter) Class.forName(formatter).newInstance());
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
fh.setFormatter(new FileLogFormatter());
}
getBaseLogger().addHandler(fh);
loggerFile = logfile.toString();
ConsoleHandler ch = new ConsoleHandler();
ch.setLevel(consolelevel);
ch.setFormatter(new ConsoleFormatter());
getBaseLogger().addHandler(ch);
} catch (IOException | SecurityException ex1) {
LOGGER.log(Level.SEVERE, "Exception logging", ex1);
throw new IOException("Exception setting up logging " + ex1);
}
return logpath;
}
use of java.util.logging.FileHandler in project robovm by robovm.
the class OldFileHandlerTest method testInvalidParams.
// This test fails on RI. Doesn't parse special pattern \"%t/%h."
public void testInvalidParams() throws IOException {
// bad directory, IOException, append
try {
new FileHandler("%t/baddir/multi%g", true);
fail("should throw IO exception");
} catch (IOException e) {
}
File file = new File(TEMPPATH + SEP + "baddir" + SEP + "multi0");
assertFalse(file.exists());
try {
new FileHandler("%t/baddir/multi%g", false);
fail("should throw IO exception");
} catch (IOException e) {
}
file = new File(TEMPPATH + SEP + "baddir" + SEP + "multi0");
assertFalse(file.exists());
try {
new FileHandler("%t/baddir/multi%g", 12, 4);
fail("should throw IO exception");
} catch (IOException e) {
}
file = new File(TEMPPATH + SEP + "baddir" + SEP + "multi0");
assertFalse(file.exists());
try {
new FileHandler("%t/java%u", -1, -1);
fail("should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
}
use of java.util.logging.FileHandler in project robovm by robovm.
the class OldFileHandlerTest method testFileHandler_1params.
public void testFileHandler_1params() throws Exception {
handler = new FileHandler("%t/log/string");
assertEquals("character encoding is non equal to actual value", "iso-8859-1", handler.getEncoding());
assertNotNull("Filter is null", handler.getFilter());
assertNotNull("Formatter is null", handler.getFormatter());
assertEquals("is non equal to actual value", Level.FINE, handler.getLevel());
assertNotNull("ErrorManager is null", handler.getErrorManager());
handler.publish(r);
handler.close();
// append mode is true
for (int i = 0; i < 3; i++) {
handler = new FileHandler("%t/log/string");
handler.publish(r);
handler.close();
}
assertFileContent(TEMPPATH + SEP + "log", "/string", new LogRecord[] { r, null, r, null, r, null, r }, new MockFormatter());
// test if unique ids not specified, it will append at the end
// no generation number is used
FileHandler h = new FileHandler("%t/log/string");
FileHandler h2 = new FileHandler("%t/log/string");
FileHandler h3 = new FileHandler("%t/log/string");
FileHandler h4 = new FileHandler("%t/log/string");
h.publish(r);
h2.publish(r);
h3.publish(r);
h4.publish(r);
h.close();
h2.close();
h3.close();
h4.close();
assertFileContent(TEMPPATH + SEP + "log", "string", h.getFormatter());
assertFileContent(TEMPPATH + SEP + "log", "string.1", h.getFormatter());
assertFileContent(TEMPPATH + SEP + "log", "string.2", h.getFormatter());
assertFileContent(TEMPPATH + SEP + "log", "string.3", h.getFormatter());
// default is append mode
FileHandler h6 = new FileHandler("%t/log/string%u.log");
h6.publish(r);
h6.close();
FileHandler h7 = new FileHandler("%t/log/string%u.log");
h7.publish(r);
h7.close();
try {
assertFileContent(TEMPPATH + SEP + "log", "string0.log", h.getFormatter());
fail("should assertion failed");
} catch (Error e) {
}
File file = new File(TEMPPATH + SEP + "log");
assertTrue("length list of file is incorrect", file.list().length <= 2);
// test unique ids
FileHandler h8 = new FileHandler("%t/log/%ustring%u.log");
h8.publish(r);
FileHandler h9 = new FileHandler("%t/log/%ustring%u.log");
h9.publish(r);
h9.close();
h8.close();
assertFileContent(TEMPPATH + SEP + "log", "0string0.log", h.getFormatter());
assertFileContent(TEMPPATH + SEP + "log", "1string1.log", h.getFormatter());
file = new File(TEMPPATH + SEP + "log");
assertTrue("length list of file is incorrect", file.list().length <= 2);
try {
new FileHandler("");
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException e) {
//expected
}
}
use of java.util.logging.FileHandler in project robovm by robovm.
the class OldFileHandlerTest method testFileHandler_2params.
public void testFileHandler_2params() throws Exception {
boolean append = false;
do {
append = !append;
handler = new FileHandler("%t/log/string", append);
assertEquals("character encoding is non equal to actual value", "iso-8859-1", handler.getEncoding());
assertNotNull("Filter is null", handler.getFilter());
assertNotNull("Formatter is null", handler.getFormatter());
assertEquals("is non equal to actual value", Level.FINE, handler.getLevel());
assertNotNull("ErrorManager is null", handler.getErrorManager());
handler.publish(r);
handler.close();
// append mode is true
for (int i = 0; i < 3; i++) {
handler = new FileHandler("%t/log/string", append);
handler.publish(r);
handler.close();
}
if (append) {
assertFileContent(TEMPPATH + SEP + "log", "/string", new LogRecord[] { r, null, r, null, r, null, r }, new MockFormatter());
} else {
assertFileContent(TEMPPATH + SEP + "log", "/string", new LogRecord[] { r }, new MockFormatter());
}
} while (append);
try {
new FileHandler("", true);
fail("IllegalArgumentException expected");
} catch (IllegalArgumentException e) {
//expected
}
}
Aggregations