本文整理汇总了Java中org.apache.log4j.Appender类的典型用法代码示例。如果您正苦于以下问题:Java Appender类的具体用法?Java Appender怎么用?Java Appender使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Appender类属于org.apache.log4j包,在下文中一共展示了Appender类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import org.apache.log4j.Appender; //导入依赖的package包/类
void run() {
Logger rootLogger = Logger.getRootLogger();
List<LoggingEvent> events = new ArrayList<>();
Appender appender = new MockUp<Appender>() {
@Mock
public void doAppend(LoggingEvent event) {
events.add(event);
}
}.getMockInstance();
rootLogger.addAppender(appender);
doRun(events);
rootLogger.removeAppender(appender);
}
开发者ID:apache,项目名称:incubator-servicecomb-java-chassis,代码行数:17,代码来源:TestServiceRegistryClientImpl.java
示例2: Log4jLoggerAdapter
import org.apache.log4j.Appender; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public Log4jLoggerAdapter() {
try {
org.apache.log4j.Logger logger = LogManager.getRootLogger();
if (logger != null) {
Enumeration<Appender> appenders = logger.getAllAppenders();
if (appenders != null) {
while (appenders.hasMoreElements()) {
Appender appender = appenders.nextElement();
if (appender instanceof FileAppender) {
FileAppender fileAppender = (FileAppender)appender;
String filename = fileAppender.getFile();
file = new File(filename);
break;
}
}
}
}
} catch (Throwable t) {
}
}
示例3: enableAsyncAuditLog
import org.apache.log4j.Appender; //导入依赖的package包/类
private static void enableAsyncAuditLog() {
if (!(auditLog instanceof Log4JLogger)) {
LOG.warn("Log4j is required to enable async auditlog");
return;
}
Logger logger = ((Log4JLogger)auditLog).getLogger();
@SuppressWarnings("unchecked")
List<Appender> appenders = Collections.list(logger.getAllAppenders());
// failsafe against trying to async it more than once
if (!appenders.isEmpty() && !(appenders.get(0) instanceof AsyncAppender)) {
AsyncAppender asyncAppender = new AsyncAppender();
// change logger to have an async appender containing all the
// previously configured appenders
for (Appender appender : appenders) {
logger.removeAppender(appender);
asyncAppender.addAppender(appender);
}
logger.addAppender(asyncAppender);
}
}
示例4: LogPageHandler
import org.apache.log4j.Appender; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public LogPageHandler() {
try {
org.apache.log4j.Logger logger = LogManager.getRootLogger();
if (logger != null) {
Enumeration<Appender> appenders = logger.getAllAppenders();
if (appenders != null) {
while (appenders.hasMoreElements()) {
Appender appender = appenders.nextElement();
if (appender instanceof FileAppender) {
FileAppender fileAppender = (FileAppender)appender;
String filename = fileAppender.getFile();
file = new File(filename);
break;
}
}
}
}
} catch (Throwable t) {
}
}
示例5: getCurrentInstance
import org.apache.log4j.Appender; //导入依赖的package包/类
/**
* This method doesn't create a new instance,
* but returns the already created one (from log4j) or null if there is no such.
*
* @return the current DB appender instance
*/
@SuppressWarnings( "unchecked")
public static ActiveDbAppender getCurrentInstance() {
if (instance == null) {
Enumeration<Appender> appenders = Logger.getRootLogger().getAllAppenders();
while (appenders.hasMoreElements()) {
Appender appender = appenders.nextElement();
if (appender instanceof ActiveDbAppender) {
instance = (ActiveDbAppender) appender;
}
}
}
return instance;
}
示例6: getCurrentInstance
import org.apache.log4j.Appender; //导入依赖的package包/类
/**
* This method doesn't create a new instance,
* but returns the already created one or null if there is no such.
*
* @return the current DB appender instance
*/
@SuppressWarnings( "unchecked")
public static PassiveDbAppender getCurrentInstance(
String caller ) {
Enumeration<Appender> appenders = Logger.getRootLogger().getAllAppenders();
while (appenders.hasMoreElements()) {
Appender appender = appenders.nextElement();
if (appender instanceof PassiveDbAppender) {
PassiveDbAppender passiveAppender = (PassiveDbAppender) appender;
if (passiveAppender.getCaller().equals(caller)) {
return passiveAppender;
}
}
}
return null;
}
示例7: Property
import org.apache.log4j.Appender; //导入依赖的package包/类
public Property() {
PropertyConfigurator.configure(this.getClass().getResourceAsStream("log4j_custom.properties"));
Logger l1 = Logger.getLogger("P2P");
Appender a = new CustomAppender();
l1.addAppender(a);
l1 = Logger.getLogger("BlockchainManager");
l1.addAppender(a);
l1 = Logger.getLogger("DbManager");
l1.addAppender(a);
l1 = Logger.getLogger("CrypDist");
l1.addAppender(a);
try {
Config.PRIVATE_KEY = new String((IOUtils.toByteArray(getClass().getResourceAsStream("private.pem"))) );
} catch (Exception e) {
}
}
示例8: setupLogger
import org.apache.log4j.Appender; //导入依赖的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());
}
示例9: LogUtil
import org.apache.log4j.Appender; //导入依赖的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();
}
}
}
示例10: syncLogs
import org.apache.log4j.Appender; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public synchronized static void syncLogs(String logLocation,
TaskAttemptID taskid,
boolean isCleanup)
throws IOException {
System.out.flush();
System.err.flush();
Enumeration<Logger> allLoggers = LogManager.getCurrentLoggers();
while (allLoggers.hasMoreElements()) {
Logger l = allLoggers.nextElement();
Enumeration<Appender> allAppenders = l.getAllAppenders();
while (allAppenders.hasMoreElements()) {
Appender a = allAppenders.nextElement();
if (a instanceof TaskLogAppender) {
((TaskLogAppender)a).flush();
}
}
}
if (currentTaskid != taskid) {
currentTaskid = taskid;
resetPrevLengths(logLocation);
}
writeToIndexFile(logLocation, isCleanup);
}
示例11: makeMetricsLoggerAsync
import org.apache.log4j.Appender; //导入依赖的package包/类
/**
* Make the metrics logger async and add all pre-existing appenders to the
* async appender.
*/
public static void makeMetricsLoggerAsync(Log metricsLog) {
if (!(metricsLog instanceof Log4JLogger)) {
LOG.warn("Metrics logging will not be async since "
+ "the logger is not log4j");
return;
}
org.apache.log4j.Logger logger = ((Log4JLogger) metricsLog).getLogger();
logger.setAdditivity(false); // Don't pollute actual logs with metrics dump
@SuppressWarnings("unchecked")
List<Appender> appenders = Collections.list(logger.getAllAppenders());
// failsafe against trying to async it more than once
if (!appenders.isEmpty() && !(appenders.get(0) instanceof AsyncAppender)) {
AsyncAppender asyncAppender = new AsyncAppender();
// change logger to have an async appender containing all the
// previously configured appenders
for (Appender appender : appenders) {
logger.removeAppender(appender);
asyncAppender.addAppender(appender);
}
logger.addAppender(asyncAppender);
}
}
示例12: log
import org.apache.log4j.Appender; //导入依赖的package包/类
/**
* Push and collect logs.
* @return Logs
* @throws IOException If fails
*/
private String log() throws IOException {
final org.apache.log4j.Logger root =
org.apache.log4j.Logger.getRootLogger();
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Appender appender = new ThreadAppender(
new PatternLayout("%t %p %m\n"),
baos
);
root.addAppender(appender);
try {
this.pipe.status(this.agent.push(this.events));
return baos.toString();
} finally {
root.removeAppender(appender);
}
}
示例13: reopenLogs
import org.apache.log4j.Appender; //导入依赖的package包/类
private void reopenLogs(int counter)
{
Logger root = Logger.getRootLogger();
@SuppressWarnings("unchecked")
Enumeration<Appender> rootAppenders = root.getAllAppenders();
FileAppender logOutput = (FileAppender) rootAppenders.nextElement();
// assume there's only the one, and that it's a file appender
logOutput.setFile("log/broker" + counter + ".trace");
logOutput.activateOptions();
Logger state = Logger.getLogger("State");
@SuppressWarnings("unchecked")
Enumeration<Appender> stateAppenders = state.getAllAppenders();
FileAppender stateOutput = (FileAppender) stateAppenders.nextElement();
// assume there's only the one, and that it's a file appender
stateOutput.setFile("log/broker" + counter + ".state");
stateOutput.activateOptions();
}
示例14: addAppender
import org.apache.log4j.Appender; //导入依赖的package包/类
public static void addAppender() {
FileAppender fa = new FileAppender();
fa.setName("FileLogger");
String logFile = getConfHome() + System.getProperty("file.separator") + "zoofx.log";
fa.setFile(logFile);
Enumeration<Appender> allAppenders = Logger.getRootLogger().getAllAppenders();
if(allAppenders.hasMoreElements()){
Appender appender = allAppenders.nextElement();
if(appender instanceof ConsoleAppender){
ConsoleAppender ca = (ConsoleAppender)appender;
fa.setLayout(ca.getLayout());
fa.setThreshold(ca.getThreshold());
}
fa.setAppend(true);
}
fa.activateOptions();
Logger.getRootLogger().addAppender(fa);
}
示例15: setCustomLogger
import org.apache.log4j.Appender; //导入依赖的package包/类
private void setCustomLogger() {
String PATTERN = "Modified Log *** ProcessDefinitionId=%X{mdcProcessDefinitionID} executionId=%X{mdcExecutionId} mdcProcessInstanceID=%X{mdcProcessInstanceID} mdcBusinessKey=%X{mdcBusinessKey} mdcTaskId=%X{mdcTaskId} %m%n";
console.setLayout(new PatternLayout(PATTERN));
console.setThreshold(Level.DEBUG);
console.activateOptions();
console.setName("MemoryAppender");
appenders = new ArrayList<Appender>();
Enumeration<?> appendersEnum = Logger.getRootLogger().getAllAppenders();
while (appendersEnum.hasMoreElements()) {
Appender object = (Appender) appendersEnum.nextElement();
appenders.add(object);
}
removeAppenders();
Logger.getRootLogger().addAppender(console);
}