use of java.util.logging.Handler in project jetty.project by eclipse.
the class JavaUtilLogTest method restoreJUL.
@AfterClass
public static void restoreJUL() {
LogManager lmgr = LogManager.getLogManager();
java.util.logging.Logger root = lmgr.getLogger("");
// Remove test handlers
for (Handler existing : root.getHandlers()) {
root.removeHandler(existing);
}
// Restore original handlers
for (Handler original : originalHandlers) {
root.addHandler(original);
}
}
use of java.util.logging.Handler in project OpenGrok by OpenGrok.
the class LoggerUtil method setFileHandlerLogPath.
public static void setFileHandlerLogPath(String path) throws IOException {
if (path != null) {
File jlp = new File(path);
if (!jlp.exists() && !jlp.mkdirs()) {
throw new IOException("could not make logpath: " + jlp.getAbsolutePath());
}
}
StringBuilder logfile = new StringBuilder();
logfile.append(path == null ? "%t" : path);
logfile.append(File.separatorChar).append("opengrok%g.%u.log");
for (Handler handler : getBaseLogger().getHandlers()) {
if (handler instanceof FileHandler) {
FileHandler fileHandler = (FileHandler) handler;
FileHandler newFileHandler;
try {
int logFilesSizeLimit = loggerIntProperty("java.util.logging.FileHandler.limit", DEFAULT_FILEHANDLER_LIMIT);
int logFilesCount = loggerIntProperty("java.util.logging.FileHandler.count", DEFAULT_FILEHANDLER_COUNT);
newFileHandler = new FileHandler(logfile.toString(), logFilesSizeLimit, logFilesCount);
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Cannot create new logger FileHandler: " + logfile.toString(), e);
return;
}
String formatter = LogManager.getLogManager().getProperty("java.util.logging.FileHandler.formatter");
newFileHandler.setLevel(fileHandler.getLevel());
try {
newFileHandler.setFormatter((Formatter) Class.forName(formatter).newInstance());
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
newFileHandler.setFormatter(new FileLogFormatter());
}
getBaseLogger().addHandler(newFileHandler);
getBaseLogger().removeHandler(fileHandler);
loggerFile = logfile.toString();
}
}
}
use of java.util.logging.Handler in project jersey by jersey.
the class ResourceBundleTest method testBadResource.
@Test
public void testBadResource() throws Exception {
final ResourceConfig resourceConfig = new ResourceConfig(BadResource.class);
ByteArrayOutputStream logOutput = new ByteArrayOutputStream();
Handler logHandler = new StreamHandler(logOutput, new SimpleFormatter());
GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig, false);
// TODO: there should be a better way to get the log output!
final Enumeration<String> loggerNames = LogManager.getLogManager().getLoggerNames();
while (loggerNames.hasMoreElements()) {
String name = loggerNames.nextElement();
if (name.startsWith("org.glassfish")) {
LogManager.getLogManager().getLogger(Errors.class.getName()).addHandler(logHandler);
}
}
GrizzlyHttpServerFactory.createHttpServer(baseUri, resourceConfig, false);
logOutput.flush();
final String logOutputAsString = logOutput.toString();
Assert.assertFalse(logOutputAsString.contains("[failed to localize]"));
Assert.assertTrue(logOutputAsString.contains("BadResource"));
}
use of java.util.logging.Handler in project hudson-2.x by hudson.
the class WeakLogHandler method setErrorManager.
@Override
public void setErrorManager(ErrorManager em) {
super.setErrorManager(em);
Handler t = resolve();
if (t != null)
t.setErrorManager(em);
}
use of java.util.logging.Handler in project randomizedtesting by randomizedtesting.
the class WithNestedTestClass method setupNested.
@BeforeClass
public static final void setupNested() throws IOException {
runningNested = true;
zombieToken = new Object();
// capture sysout/ syserr.
sw = new StringWriter();
sysout = System.out;
syserr = System.err;
System.setOut(new PrintStream(new TeeOutputStream(System.out, new WriterOutputStream(sw))));
System.setErr(new PrintStream(new TeeOutputStream(System.err, new WriterOutputStream(sw))));
// Add custom logging handler because java logging keeps a reference to previous System.err.
loggingMessages = new StringWriter();
logger = Logger.getLogger("");
handlers = logger.getHandlers();
for (Handler h : handlers) logger.removeHandler(h);
logger.addHandler(new Handler() {
final SimpleFormatter formatter = new SimpleFormatter();
@Override
public void publish(LogRecord record) {
loggingMessages.write(formatter.format(record) + "\n");
}
@Override
public void flush() {
}
@Override
public void close() throws SecurityException {
}
});
}
Aggregations