本文整理匯總了Java中org.apache.log4j.PatternLayout類的典型用法代碼示例。如果您正苦於以下問題:Java PatternLayout類的具體用法?Java PatternLayout怎麽用?Java PatternLayout使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
PatternLayout類屬於org.apache.log4j包,在下文中一共展示了PatternLayout類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: initLogger
import org.apache.log4j.PatternLayout; //導入依賴的package包/類
private static void initLogger() {
Logger rootLogger = Logger.getRootLogger();
PatternLayout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n");
try {
RollingFileAppender fileAppender = new RollingFileAppender(layout, System.getProperty("log.file", "./vnt.log"));
fileAppender.setImmediateFlush(true);
fileAppender.setThreshold(Level.DEBUG);
fileAppender.setAppend(true);
fileAppender.setMaxFileSize("5MB");
fileAppender.setMaxBackupIndex(2);
rootLogger.addAppender(fileAppender);
} catch (IOException e) {
log.error("Failed to add appender !!", e);
}
}
示例2: initLogger
import org.apache.log4j.PatternLayout; //導入依賴的package包/類
private RollingFileAppender initLogger(String loggerName, String filePrefix) {
String fileName = Paths.get(rootPath, filePrefix + "." + loggerName + ".dat").toString();
RollingFileAppender fileAppender = new RollingFileAppender();
fileAppender.setName(loggerName);
fileAppender.setFile(fileName);
fileAppender.setLayout(new PatternLayout("%m%n"));
fileAppender.setAppend(true);
fileAppender.setMaxFileSize(maxFileSize);
fileAppender.setMaxBackupIndex(maxFileCount);
fileAppender.activateOptions();
return fileAppender;
}
示例3: setUpClass
import org.apache.log4j.PatternLayout; //導入依賴的package包/類
@BeforeClass
public static void setUpClass() throws Exception {
//Configure log4j
ConsoleAppender console = new ConsoleAppender();
String PATTERN = "%d [%p|%c|%C{1}] %m%n";
console.setLayout(new PatternLayout(PATTERN));
console.setThreshold(Level.DEBUG);
console.activateOptions();
Logger.getRootLogger().addAppender(console);
System.out.println("SETUP: Starting SPLGroundControl...");
config.init(args);
daemon.init(new SPLGroundControl.SPLDaemonContext(args));
daemon.start();
}
示例4: setupLogging
import org.apache.log4j.PatternLayout; //導入依賴的package包/類
private void setupLogging() {
LogManager.getLogManager().reset();
Logger.getRootLogger().getLoggerRepository().resetConfiguration();
final Logger rootLogger = Logger.getRootLogger();
final PatternLayout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n");
final Level level = endPointHealthConfiguration.logLevel();
final ConsoleAppender consoleAppender = new ConsoleAppender(layout, ConsoleAppender.SYSTEM_ERR);
consoleAppender.setThreshold(level);
consoleAppender.activateOptions();
final FileAppender fileAppender = new FileAppender();
fileAppender.setFile(endPointHealthConfiguration.logFile().getAbsolutePath());
fileAppender.setLayout(layout);
fileAppender.setThreshold(level);
fileAppender.setAppend(true);
fileAppender.setErrorHandler(new FallbackAppenderErrorHandler(rootLogger, fileAppender, consoleAppender));
rootLogger.addAppender(fileAppender);
fileAppender.activateOptions();
}
示例5: attachPassiveDbAppender
import org.apache.log4j.PatternLayout; //導入依賴的package包/類
private void attachPassiveDbAppender(
DbAppenderConfiguration appenderConfiguration,
long timestamp ) {
// create the new appender
PassiveDbAppender attachedAppender = new PassiveDbAppender(ThreadsPerCaller.getCaller());
// calculate the time stamp offset, between the test executor and the agent
attachedAppender.calculateTimeOffset(timestamp);
attachedAppender.setAppenderConfig(appenderConfiguration);
// use a default pattern, as we log in the db
attachedAppender.setLayout(new PatternLayout("%c{2}: %m%n"));
attachedAppender.activateOptions();
// attach the appender to the logging system
Category log = Logger.getRootLogger();
log.setLevel(Level.toLevel(appenderConfiguration.getLoggingThreshold().toInt()));
log.addAppender(attachedAppender);
}
示例6: testAsciiMessageFromEventDefaultLayout
import org.apache.log4j.PatternLayout; //導入依賴的package包/類
@Test
public void testAsciiMessageFromEventDefaultLayout() throws Exception
{
final long timestamp = System.currentTimeMillis();
final String text = "test";
LoggingEvent event = createLoggingEvent(timestamp, text, null);
LogMessage message = new LogMessage(event, new PatternLayout());
// the default pattern appends a newline
String expectedText = text + "\n";
assertEquals("timestmap", timestamp, message.getTimestamp());
assertEquals("message", expectedText, message.getMessage());
assertArrayEquals("message as bytes", expectedText.getBytes("UTF-8"), message.getBytes());
assertEquals("size", 5, message.size());
}
示例7: testUnicodeMessageFromEventDefaultLayout
import org.apache.log4j.PatternLayout; //導入依賴的package包/類
@Test
public void testUnicodeMessageFromEventDefaultLayout() throws Exception
{
final long timestamp = System.currentTimeMillis();
final String text = "\u0024\u00a2\u20ac";
LoggingEvent event = createLoggingEvent(timestamp, text, null);
LogMessage message = new LogMessage(event, new PatternLayout());
// the default pattern appends a newline
String expectedText = text + "\n";
assertEquals("timestmap", timestamp, message.getTimestamp());
assertEquals("message", expectedText, message.getMessage());
assertArrayEquals("message as bytes", expectedText.getBytes("UTF-8"), message.getBytes());
assertEquals("size", 7, message.size());
}
示例8: testAsciiMessageFromEventDefaultLayoutWithException
import org.apache.log4j.PatternLayout; //導入依賴的package包/類
@Test
// at this point we'll assume that UTF-8 conversion works as expected
public void testAsciiMessageFromEventDefaultLayoutWithException() throws Exception
{
final long timestamp = System.currentTimeMillis();
final String text = "test";
final Exception ex = new Exception();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
pw.println(text);
ex.printStackTrace(pw);
pw.close();
String expectedText = sw.toString();
LoggingEvent event = createLoggingEvent(timestamp, text, ex);
LogMessage message = new LogMessage(event, new PatternLayout());
assertEquals("explicit timestamp", timestamp, message.getTimestamp());
assertEquals("message as string", expectedText, message.getMessage());
}
示例9: setUp
import org.apache.log4j.PatternLayout; //導入依賴的package包/類
public void setUp() {
Logger root = Logger.getRootLogger();
root.setLevel(Level.DEBUG);
root.addAppender(new ConsoleAppender(
new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN)));
// setup TLS properties
System.setProperty( "javax.net.ssl.keyStore", TlsTest.class.getResource("testkeys").getPath() );
System.setProperty( "javax.net.ssl.trustStore", TlsTest.class.getResource("testkeys").getPath() );
System.setProperty( "javax.net.ssl.keyStorePassword", "passphrase" );
System.setProperty( "javax.net.ssl.keyStoreType", "jks" );
this.shootist = new BadShootist();
this.shootme = new BadShootme();
shootist.setSocketDisconnectWorstCase(true);
shootme.setSocketDisconnectWorstCase(true);
this.shootme.init();
}
示例10: main
import org.apache.log4j.PatternLayout; //導入依賴的package包/類
public static void main(String[] args) throws TaskExecutionException {
ConsoleAppender ca = new ConsoleAppender();
ca.setWriter(new OutputStreamWriter(System.out));
ca.setLayout(new PatternLayout("%-5p [%t]: %m%n"));
ca.setThreshold(Level.TRACE);
logger.getRootLogger().addAppender(ca);
final MarkLogicMonitor monitor = new MarkLogicMonitor();
final Map<String, String> taskArgs = new HashMap<String, String>();
taskArgs.put("config-file", "src/main/resources/conf/config.yml");
taskArgs.put("metrics-file", "src/main/resources/conf/metrics.xml");
ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
scheduler.scheduleAtFixedRate(new Runnable() {
public void run() {
try {
monitor.execute(taskArgs, null);
} catch (Exception e) {
logger.error("Error while running the task", e);
}
}
}, 2, 30, TimeUnit.SECONDS);
}
示例11: setUpExperimentalLogger
import org.apache.log4j.PatternLayout; //導入依賴的package包/類
private static void setUpExperimentalLogger(File pathF) throws IOException {
Logger.getRootLogger().removeAllAppenders();
log.removeAllAppenders();
PropertyConfigurator.configure("resource/log4jproperties.txt");
FileAppender resultAppender = new FileAppender(
new PatternLayout("%m"),
new File(pathF, "result.txt").toString(), false);
LevelRangeFilter infoFilter = new LevelRangeFilter();
infoFilter.setLevelMin(Level.INFO);
infoFilter.setLevelMax(Level.FATAL);
infoFilter.setAcceptOnMatch(true);
resultAppender.addFilter(infoFilter);
log.addAppender(resultAppender);
Logger.getLogger(iBench.class).addAppender(resultAppender);
}
示例12: setUpConfigLogger
import org.apache.log4j.PatternLayout; //導入依賴的package包/類
private static void setUpConfigLogger(File pathF, String configName) throws IOException {
Logger.getRootLogger().removeAllAppenders();
String nameSuffix = configName.replace(".txt", "");
PropertyConfigurator.configure("resource/log4jproperties.txt");
FileAppender logFileAppender = new FileAppender(
new PatternLayout("%-4r [%t] %-5p %c %x - %m%n"),
new File(pathF, "log_" + nameSuffix + ".txt").toString(), false);
Logger.getRootLogger().addAppender(logFileAppender);
if (opt.logToConsole)
Logger.getRootLogger().addAppender(new ConsoleAppender(
new PatternLayout("%-4r [%t] %-5p %c %x - %m%n")));
}
示例13: setupLogger
import org.apache.log4j.PatternLayout; //導入依賴的package包/類
public static void setupLogger(Config config) throws IOException {
Layout layout = new PatternLayout("%d{" + DATE_FORMAT + "} %5p: %m%n");
Appender appender = new DailyRollingFileAppender(
layout, config.getString("logger.file"), "'.'yyyyMMdd");
LogManager.resetConfiguration();
LogManager.getRootLogger().addAppender(new NullAppender());
logger = Logger.getLogger(LOGGER_NAME);
logger.addAppender(appender);
logger.setLevel(Level.toLevel(config.getString("logger.level"), Level.ALL));
// Workaround for "Bug 745866 - (EDG-45) Possible netty logging config problem"
InternalLoggerFactory.setDefaultFactory(new InternalLoggerFactory() {
@Override
public InternalLogger newInstance(String string) {
return new NettyInternalLogger();
}
});
Log.logSystemInfo();
Log.info("Version: " + getAppVersion());
}
示例14: LogUtil
import org.apache.log4j.PatternLayout; //導入依賴的package包/類
public LogUtil(LogConfig config, Object obj){
this.logConfig = config;
if(LogConfig.logType == 0){
report = new ExtentReports(config.getExtentLogPath(),false);
File configFile = new File(System.getProperty("user.dir") + "\\configs\\extent-config.xml");
if(configFile.exists()) {
report.loadConfig(configFile);
}
testLog = report.startTest(String.valueOf(obj));
}else{
logger = Logger.getLogger(String.valueOf(obj));
Layout layout = new PatternLayout("%d{yyyy-MM-dd HH:mm:ss} [%p] %c:%L �� %m%n");
try {
Appender appender = new FileAppender(layout, config.getLog4jPath());
logger.addAppender(appender);
} catch (IOException e) {
e.printStackTrace();
}
}
}
示例15: testAppendInClose
import org.apache.log4j.PatternLayout; //導入依賴的package包/類
@Test
public void testAppendInClose() throws Exception {
final ContainerLogAppender claAppender = new ContainerLogAppender();
claAppender.setName("testCLA");
claAppender.setLayout(new PatternLayout("%-5p [%t]: %m%n"));
claAppender.setContainerLogDir("target/testAppendInClose/logDir");
claAppender.setContainerLogFile("syslog");
claAppender.setTotalLogFileSize(1000);
claAppender.activateOptions();
final Logger claLog = Logger.getLogger("testAppendInClose-catergory");
claLog.setAdditivity(false);
claLog.addAppender(claAppender);
claLog.info(new Object() {
public String toString() {
claLog.info("message1");
return "return message1";
}
});
claAppender.close();
}