use of java.util.logging.ErrorManager in project tomee by apache.
the class LocalFileHandler method configure.
private void configure() {
date = currentDate();
//allow classes to override
final String className = getClass().getName();
final ClassLoader cl = Thread.currentThread().getContextClassLoader();
dateCheckInterval = new Duration(getProperty(className + ".dateCheckInterval", String.valueOf(dateCheckInterval))).asMillis();
filenamePattern = replace(getProperty(className + ".filenamePattern", filenamePattern));
limit = new Size(getProperty(className + ".limit", String.valueOf("10 Mega"))).asBytes();
final int lastSep = Math.max(filenamePattern.lastIndexOf('/'), filenamePattern.lastIndexOf('\\'));
String fileNameReg = lastSep >= 0 ? filenamePattern.substring(lastSep + 1) : filenamePattern;
// date.
fileNameReg = fileNameReg.replace("%s", "\\d{4}\\-\\d{2}\\-\\d{2}");
{
// file rotation index
final int indexIdxStart = fileNameReg.indexOf('%');
if (indexIdxStart >= 0) {
final int indexIdxEnd = fileNameReg.indexOf('d', indexIdxStart);
if (indexIdxEnd >= 0) {
fileNameReg = fileNameReg.substring(0, indexIdxStart) + "\\d*" + fileNameReg.substring(indexIdxEnd + 1, fileNameReg.length());
}
}
}
filenameRegex = Pattern.compile(fileNameReg);
compressionLevel = Integer.parseInt(getProperty(className + ".compressionLevel", String.valueOf(Deflater.DEFAULT_COMPRESSION)));
archiveExpiryDuration = new Duration(getProperty(className + ".archiveOlderThan", String.valueOf("-1 days"))).asMillis();
archiveDir = new File(replace(getProperty(className + ".archiveDirectory", "${catalina.base}/logs/archives/")));
archiveFormat = replace(getProperty(className + ".archiveFormat", archiveFormat));
archiveFilenameRegex = Pattern.compile(fileNameReg + "\\." + archiveFormat);
purgeExpiryDuration = new Duration(getProperty(className + ".purgeOlderThan", String.valueOf("-1 days"))).asMillis();
try {
bufferSize = (int) new Size(getProperty(className + ".bufferSize", "-1 b")).asBytes();
} catch (final NumberFormatException ignore) {
// no-op
}
final String encoding = getProperty(className + ".encoding", null);
if (encoding != null && encoding.length() > 0) {
try {
setEncoding(encoding);
} catch (final UnsupportedEncodingException ex) {
// no-op
}
}
setLevel(Level.parse(getProperty(className + ".level", "" + Level.ALL)));
final String filterName = getProperty(className + ".filter", null);
if (filterName != null) {
try {
setFilter(Filter.class.cast(cl.loadClass(filterName).newInstance()));
} catch (final Exception e) {
// Ignore
}
}
final String formatterName = getProperty(className + ".formatter", null);
if (formatterName != null) {
try {
setFormatter(Formatter.class.cast(cl.loadClass(formatterName).newInstance()));
} catch (final Exception e) {
setFormatter(newSimpleFormatter(className));
}
} else {
setFormatter(newSimpleFormatter(className));
}
setErrorManager(new ErrorManager());
lastTimestamp = System.currentTimeMillis();
}
use of java.util.logging.ErrorManager in project tomcat by apache.
the class FileHandler method configure.
/**
* Configure from <code>LogManager</code> properties.
*/
private void configure() {
Timestamp ts = new Timestamp(System.currentTimeMillis());
String tsString = ts.toString().substring(0, 19);
date = tsString.substring(0, 10);
//allow classes to override
String className = this.getClass().getName();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
// Retrieve configuration of logging file name
rotatable = Boolean.parseBoolean(getProperty(className + ".rotatable", "true"));
if (directory == null)
directory = getProperty(className + ".directory", "logs");
if (prefix == null)
prefix = getProperty(className + ".prefix", "juli.");
if (suffix == null)
suffix = getProperty(className + ".suffix", ".log");
String sBufferSize = getProperty(className + ".bufferSize", String.valueOf(bufferSize));
try {
bufferSize = Integer.parseInt(sBufferSize);
} catch (NumberFormatException ignore) {
//no op
}
// Get encoding for the logging file
String encoding = getProperty(className + ".encoding", null);
if (encoding != null && encoding.length() > 0) {
try {
setEncoding(encoding);
} catch (UnsupportedEncodingException ex) {
// Ignore
}
}
// Get logging level for the handler
setLevel(Level.parse(getProperty(className + ".level", "" + Level.ALL)));
// Get filter configuration
String filterName = getProperty(className + ".filter", null);
if (filterName != null) {
try {
setFilter((Filter) cl.loadClass(filterName).newInstance());
} catch (Exception e) {
// Ignore
}
}
// Set formatter
String formatterName = getProperty(className + ".formatter", null);
if (formatterName != null) {
try {
setFormatter((Formatter) cl.loadClass(formatterName).newInstance());
} catch (Exception e) {
// Ignore and fallback to defaults
setFormatter(new OneLineFormatter());
}
} else {
setFormatter(new OneLineFormatter());
}
// Set error manager
setErrorManager(new ErrorManager());
}
use of java.util.logging.ErrorManager in project j2objc by google.
the class HandlerTest method testConstructor_Properties.
/*
* Test the constructor, with properties set
*/
public void testConstructor_Properties() throws Exception {
Properties p = new Properties();
p.put("java.util.logging.MockHandler.level", "FINE");
p.put("java.util.logging.MockHandler.filter", className + "$MockFilter");
p.put("java.util.logging.Handler.formatter", className + "$MockFormatter");
p.put("java.util.logging.MockHandler.encoding", "utf-8");
LogManager.getLogManager().readConfiguration(EnvironmentHelper.PropertiesToInputStream(p));
assertEquals(LogManager.getLogManager().getProperty("java.util.logging.MockHandler.level"), "FINE");
assertEquals(LogManager.getLogManager().getProperty("java.util.logging.MockHandler.encoding"), "utf-8");
MockHandler h = new MockHandler();
assertSame(h.getLevel(), Level.ALL);
assertNull(h.getFormatter());
assertNull(h.getFilter());
assertNull(h.getEncoding());
assertTrue(h.getErrorManager() instanceof ErrorManager);
LogManager.getLogManager().reset();
}
use of java.util.logging.ErrorManager in project google-cloud-java by GoogleCloudPlatform.
the class LoggingHandlerTest method testReportFormatError.
@Test
public void testReportFormatError() {
expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
expect(options.getService()).andReturn(logging);
logging.setFlushSeverity(Severity.ERROR);
expectLastCall().once();
logging.setWriteSynchronicity(Synchronicity.ASYNC);
expectLastCall().once();
replay(options, logging);
Formatter formatter = EasyMock.createStrictMock(Formatter.class);
RuntimeException ex = new RuntimeException();
ErrorManager errorManager = EasyMock.createStrictMock(ErrorManager.class);
errorManager.error(null, ex, ErrorManager.FORMAT_FAILURE);
expectLastCall().once();
LogRecord record = newLogRecord(Level.FINEST, MESSAGE);
expect(formatter.format(record)).andThrow(ex);
replay(errorManager, formatter);
Handler handler = new LoggingHandler(LOG_NAME, options);
handler.setLevel(Level.ALL);
handler.setErrorManager(errorManager);
handler.setFormatter(formatter);
handler.publish(record);
verify(errorManager, formatter);
}
use of java.util.logging.ErrorManager in project google-cloud-java by GoogleCloudPlatform.
the class LoggingHandlerTest method testReportFlushError.
@Test
public void testReportFlushError() {
expect(options.getProjectId()).andReturn(PROJECT).anyTimes();
expect(options.getService()).andReturn(logging);
RuntimeException ex = new RuntimeException();
logging.setFlushSeverity(Severity.ERROR);
expectLastCall().once();
logging.setWriteSynchronicity(Synchronicity.ASYNC);
expectLastCall().once();
logging.write(ImmutableList.of(FINEST_ENTRY), DEFAULT_OPTIONS);
expectLastCall().once();
logging.flush();
expectLastCall().andThrow(ex);
ErrorManager errorManager = EasyMock.createStrictMock(ErrorManager.class);
errorManager.error(null, ex, ErrorManager.FLUSH_FAILURE);
expectLastCall().once();
replay(options, logging, errorManager);
LogRecord record = newLogRecord(Level.FINEST, MESSAGE);
Handler handler = new LoggingHandler(LOG_NAME, options);
handler.setLevel(Level.ALL);
handler.setErrorManager(errorManager);
handler.setFormatter(new TestFormatter());
handler.publish(record);
handler.flush();
verify(errorManager);
}
Aggregations