use of org.apache.logging.log4j.core.appender.FileAppender in project logging-log4j2 by apache.
the class Log4j1ConfigurationFactoryTest method testFile.
private Layout<?> testFile(final String configResource) throws Exception {
final Configuration configuration = getConfiguration(configResource);
final FileAppender appender = configuration.getAppender("File");
assertNotNull(appender);
assertEquals("target/mylog.txt", appender.getFileName());
//
final LoggerConfig loggerConfig = configuration.getLoggerConfig("com.example.foo");
assertNotNull(loggerConfig);
assertEquals(Level.DEBUG, loggerConfig.getLevel());
configuration.start();
configuration.stop();
return appender.getLayout();
}
use of org.apache.logging.log4j.core.appender.FileAppender in project logging-log4j2 by apache.
the class PatternLayoutMainMapLookupTest method testFileName.
@Test
public void testFileName() {
final FileAppender fileApp = (FileAppender) context.getRequiredAppender("File");
final String name = fileApp.getFileName();
Assert.assertEquals("target/value0.log", name);
}
use of org.apache.logging.log4j.core.appender.FileAppender in project cas by apereo.
the class LoggingConfigController method getConfiguration.
/**
* Gets configuration as JSON.
* Depends on the log4j core API.
*
* @param request the request
* @param response the response
* @return the configuration
* @throws Exception the exception
*/
@GetMapping(value = "/getConfiguration")
@ResponseBody
public Map<String, Object> getConfiguration(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
ensureEndpointAccessIsAuthorized(request, response);
final Collection<Map<String, Object>> configuredLoggers = new HashSet<>();
getLoggerConfigurations().forEach(config -> {
final Map<String, Object> loggerMap = new HashMap<>();
loggerMap.put("name", StringUtils.defaultIfBlank(config.getName(), LOGGER_NAME_ROOT));
loggerMap.put("state", config.getState());
if (config.getProperties() != null) {
loggerMap.put("properties", config.getProperties());
}
loggerMap.put("additive", config.isAdditive());
loggerMap.put("level", config.getLevel().name());
final Collection<String> appenders = new HashSet<>();
config.getAppenders().keySet().stream().map(key -> config.getAppenders().get(key)).forEach(appender -> {
final ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.JSON_STYLE);
builder.append("name", appender.getName());
builder.append("state", appender.getState());
builder.append("layoutFormat", appender.getLayout().getContentFormat());
builder.append("layoutContentType", appender.getLayout().getContentType());
if (appender instanceof FileAppender) {
builder.append(FILE_PARAM, ((FileAppender) appender).getFileName());
builder.append(FILE_PATTERN_PARAM, "(none)");
}
if (appender instanceof RandomAccessFileAppender) {
builder.append(FILE_PARAM, ((RandomAccessFileAppender) appender).getFileName());
builder.append(FILE_PATTERN_PARAM, "(none)");
}
if (appender instanceof RollingFileAppender) {
builder.append(FILE_PARAM, ((RollingFileAppender) appender).getFileName());
builder.append(FILE_PATTERN_PARAM, ((RollingFileAppender) appender).getFilePattern());
}
if (appender instanceof MemoryMappedFileAppender) {
builder.append(FILE_PARAM, ((MemoryMappedFileAppender) appender).getFileName());
builder.append(FILE_PATTERN_PARAM, "(none)");
}
if (appender instanceof RollingRandomAccessFileAppender) {
builder.append(FILE_PARAM, ((RollingRandomAccessFileAppender) appender).getFileName());
builder.append(FILE_PATTERN_PARAM, ((RollingRandomAccessFileAppender) appender).getFilePattern());
}
appenders.add(builder.build());
});
loggerMap.put("appenders", appenders);
configuredLoggers.add(loggerMap);
});
final Map<String, Object> responseMap = new HashMap<>();
responseMap.put("loggers", configuredLoggers);
return responseMap;
}
use of org.apache.logging.log4j.core.appender.FileAppender in project hive by apache.
the class ExecDriver method main.
@SuppressWarnings("unchecked")
public static void main(String[] args) throws IOException, HiveException {
String planFileName = null;
String jobConfFileName = null;
boolean noLog = false;
String files = null;
String libjars = null;
boolean localtask = false;
try {
for (int i = 0; i < args.length; i++) {
if (args[i].equals("-plan")) {
planFileName = args[++i];
} else if (args[i].equals("-jobconffile")) {
jobConfFileName = args[++i];
} else if (args[i].equals("-nolog")) {
noLog = true;
} else if (args[i].equals("-files")) {
files = args[++i];
} else if (args[i].equals("-libjars")) {
libjars = args[++i];
} else if (args[i].equals("-localtask")) {
localtask = true;
}
}
} catch (IndexOutOfBoundsException e) {
System.err.println("Missing argument to option");
printUsage();
}
JobConf conf;
if (localtask) {
conf = new JobConf(MapredLocalTask.class);
} else {
conf = new JobConf(ExecDriver.class);
}
if (jobConfFileName != null) {
conf.addResource(new Path(jobConfFileName));
}
// Initialize the resources from command line
if (files != null) {
conf.set("tmpfiles", files);
}
if (libjars != null) {
conf.set("tmpjars", libjars);
}
if (UserGroupInformation.isSecurityEnabled()) {
String hadoopAuthToken = System.getenv(UserGroupInformation.HADOOP_TOKEN_FILE_LOCATION);
if (hadoopAuthToken != null) {
conf.set("mapreduce.job.credentials.binary", hadoopAuthToken);
}
}
boolean isSilent = HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVESESSIONSILENT);
String queryId = HiveConf.getVar(conf, HiveConf.ConfVars.HIVEQUERYID, "").trim();
if (queryId.isEmpty()) {
queryId = "unknown-" + System.currentTimeMillis();
HiveConf.setVar(conf, HiveConf.ConfVars.HIVEQUERYID, queryId);
}
System.setProperty(HiveConf.ConfVars.HIVEQUERYID.toString(), queryId);
LogUtils.registerLoggingContext(conf);
if (noLog) {
// If started from main(), and noLog is on, we should not output
// any logs. To turn the log on, please set -Dtest.silent=false
org.apache.logging.log4j.Logger logger = org.apache.logging.log4j.LogManager.getRootLogger();
NullAppender appender = NullAppender.createNullAppender();
appender.addToLogger(logger.getName(), Level.ERROR);
appender.start();
} else {
setupChildLog4j(conf);
}
Logger LOG = LoggerFactory.getLogger(ExecDriver.class.getName());
LogHelper console = new LogHelper(LOG, isSilent);
if (planFileName == null) {
console.printError("Must specify Plan File Name");
printUsage();
}
// that it's easy to find reason for local mode execution failures
for (Appender appender : ((org.apache.logging.log4j.core.Logger) LogManager.getRootLogger()).getAppenders().values()) {
if (appender instanceof FileAppender) {
console.printInfo("Execution log at: " + ((FileAppender) appender).getFileName());
} else if (appender instanceof RollingFileAppender) {
console.printInfo("Execution log at: " + ((RollingFileAppender) appender).getFileName());
}
}
// the plan file should always be in local directory
Path p = new Path(planFileName);
FileSystem fs = FileSystem.getLocal(conf);
InputStream pathData = fs.open(p);
// child process. so we add it here explicitly
try {
// see also - code in CliDriver.java
ClassLoader loader = conf.getClassLoader();
if (StringUtils.isNotBlank(libjars)) {
AddToClassPathAction addAction = new AddToClassPathAction(loader, Arrays.asList(StringUtils.split(libjars, ",")));
loader = AccessController.doPrivileged(addAction);
}
conf.setClassLoader(loader);
// Also set this to the Thread ContextClassLoader, so new threads will
// inherit
// this class loader, and propagate into newly created Configurations by
// those
// new threads.
Thread.currentThread().setContextClassLoader(loader);
} catch (Exception e) {
throw new HiveException(e.getMessage(), e);
}
int ret;
if (localtask) {
memoryMXBean = ManagementFactory.getMemoryMXBean();
MapredLocalWork plan = SerializationUtilities.deserializePlan(pathData, MapredLocalWork.class);
MapredLocalTask ed = new MapredLocalTask(plan, conf, isSilent);
ed.initialize(null, null, new TaskQueue(), null);
ret = ed.executeInProcess();
} else {
MapredWork plan = SerializationUtilities.deserializePlan(pathData, MapredWork.class);
ExecDriver ed = new ExecDriver(plan, conf, isSilent);
ed.setTaskQueue(new TaskQueue());
ret = ed.execute();
}
if (ret != 0) {
System.exit(ret);
}
}
use of org.apache.logging.log4j.core.appender.FileAppender in project uavstack by uavorg.
the class Log4j2HookProxy method getAppenderInfo.
private LogProfileInfo getAppenderInfo(Appender appender, String appid) {
if (appender == null) {
return null;
}
String fileName;
if (appender instanceof FileAppender) {
fileName = ((FileAppender) appender).getFileName();
} else if (appender instanceof RollingFileAppender) {
fileName = ((RollingFileAppender) appender).getFileName();
} else if (appender instanceof RollingRandomAccessFileAppender) {
fileName = ((RollingRandomAccessFileAppender) appender).getFileName();
} else if (appender instanceof RandomAccessFileAppender) {
fileName = ((RandomAccessFileAppender) appender).getFileName();
} else if (appender instanceof MemoryMappedFileAppender) {
fileName = ((MemoryMappedFileAppender) appender).getFileName();
} else {
return null;
}
Map<String, String> attributes = new HashMap<String, String>();
attributes.put(LogProfileInfo.ENGINE, "log4j2");
Layout<?> layout = appender.getLayout();
if (null != layout) {
if (layout instanceof PatternLayout) {
attributes.put(LogProfileInfo.PATTERN, ((PatternLayout) layout).getConversionPattern());
} else {
attributes.put(LogProfileInfo.PATTERN, layout.getClass().getSimpleName());
}
}
LogProfileInfo logProfileInfo = new LogProfileInfo();
logProfileInfo.setAppId(appid);
logProfileInfo.setFilePath(fileName);
logProfileInfo.setAttributes(attributes);
logProfileInfo.setLogType(LogProfileInfo.LogType.Log4j);
return logProfileInfo;
}
Aggregations