use of org.apache.log4j.FileAppender in project smscgateway by RestComm.
the class TestHarness method init.
public void init() {
try {
Properties tckProperties = new Properties();
InputStream inStreamLog4j = TestHarness.class.getResourceAsStream("/log4j.properties");
System.out.println("Input Stream = " + inStreamLog4j);
Properties propertiesLog4j = new Properties();
try {
propertiesLog4j.load(inStreamLog4j);
PropertyConfigurator.configure(propertiesLog4j);
} catch (IOException e) {
e.printStackTrace();
BasicConfigurator.configure();
}
logger.debug("log4j configured");
String lf = System.getProperties().getProperty(LOG_FILE_NAME);
if (lf != null) {
logFileName = lf;
}
// If already created a print writer then just use it.
try {
logger.addAppender(new FileAppender(new SimpleLayout(), logFileName));
} catch (FileNotFoundException fnfe) {
}
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
use of org.apache.log4j.FileAppender in project smscgateway by RestComm.
the class SMPPp method initLog4J.
private void initLog4J(Properties props) {
try {
String log4jProps = props.getProperty("smppp.log4jPropsFilePath");
if (log4jProps != null) {
InputStream inStreamLog4j = new FileInputStream(new File(log4jProps));
Properties propertiesLog4j = new Properties();
propertiesLog4j.load(inStreamLog4j);
PropertyConfigurator.configure(propertiesLog4j);
} else {
BasicConfigurator.configure();
}
String lf = props.getProperty("smppp.logFilePath");
if (lf != null) {
ctx.logger.addAppender(new FileAppender(new SimpleLayout(), lf));
}
} catch (Exception ex) {
throw new RuntimeException(ex);
}
}
use of org.apache.log4j.FileAppender in project BiglyBT by BiglySoftware.
the class Log method execute.
@Override
public void execute(String commandName, final ConsoleInput ci, CommandLine commandLine) {
Appender con = Logger.getRootLogger().getAppender("ConsoleAppender");
List args = commandLine.getArgList();
if ((con != null) && (!args.isEmpty())) {
String subcommand = (String) args.get(0);
if ("off".equalsIgnoreCase(subcommand)) {
if (args.size() == 1) {
con.addFilter(new DenyAllFilter());
ci.out.println("> Console logging off");
} else {
String name = (String) args.get(1);
Object[] entry = (Object[]) channel_listener_map.remove(name);
if (entry == null) {
ci.out.println("> Channel '" + name + "' not being logged");
} else {
((LoggerChannel) entry[0]).removeListener((LoggerChannelListener) entry[1]);
ci.out.println("> Channel '" + name + "' logging off");
}
}
} else if ("on".equalsIgnoreCase(subcommand)) {
if (args.size() == 1) {
if (commandLine.hasOption('f')) {
// send log output to a file
String filename = commandLine.getOptionValue('f');
try {
Appender newAppender = new FileAppender(new PatternLayout("%d{ISO8601} %c{1}-%p: %m%n"), filename, true);
newAppender.setName("ConsoleAppender");
Logger.getRootLogger().removeAppender(con);
Logger.getRootLogger().addAppender(newAppender);
ci.out.println("> Logging to filename: " + filename);
} catch (IOException e) {
ci.out.println("> Unable to log to file: " + filename + ": " + e);
}
} else {
if (!(con instanceof ConsoleAppender)) {
Logger.getRootLogger().removeAppender(con);
con = new ConsoleAppender(new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN));
con.setName("ConsoleAppender");
Logger.getRootLogger().addAppender(con);
}
// switch back to console appender
ci.out.println("> Console logging on");
}
con.clearFilters();
} else {
// hack - dunno how to do plugin-specific logging using these damn appenders..
Map channel_map = getChannelMap(ci);
final String name = (String) args.get(1);
LoggerChannel channel = (LoggerChannel) channel_map.get(name);
if (channel == null) {
ci.out.println("> Channel '" + name + "' not found");
} else if (channel_listener_map.get(name) != null) {
ci.out.println("> Channel '" + name + "' already being logged");
} else {
LoggerChannelListener l = new LoggerChannelListener() {
@Override
public void messageLogged(int type, String content) {
ci.out.println("[" + name + "] " + content);
}
@Override
public void messageLogged(String str, Throwable error) {
ci.out.println("[" + name + "] " + str);
error.printStackTrace(ci.out);
}
};
channel.addListener(l);
channel_listener_map.put(name, new Object[] { channel, l });
ci.out.println("> Channel '" + name + "' on");
}
}
} else if (subcommand.equalsIgnoreCase("list")) {
Map channel_map = getChannelMap(ci);
Iterator it = channel_map.keySet().iterator();
while (it.hasNext()) {
String name = (String) it.next();
ci.out.println(" " + name + " [" + (channel_listener_map.get(name) == null ? "off" : "on") + "]");
}
} else {
ci.out.println("> Command 'log': Subcommand '" + subcommand + "' unknown.");
}
} else {
ci.out.println("> Console logger not found or missing subcommand for 'log'\r\n> log syntax: log [-f filename] (on [name]|off [name]|list)");
}
}
use of org.apache.log4j.FileAppender in project atlas by apache.
the class FailedMessagesLogger method getRootLoggerDirectory.
/**
* Get the root logger file location under which the failed log messages will be written.
*
* Since this class is used in Hooks which run within JVMs of other components like Hive,
* we want to write the failed messages file under the same location as where logs from
* the host component are saved. This method attempts to get such a location from the
* root logger's appenders. It will work only if at least one of the appenders is a {@link FileAppender}
*
* @return directory under which host component's logs are stored.
*/
private String getRootLoggerDirectory() {
String rootLoggerDirectory = null;
org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
Enumeration allAppenders = rootLogger.getAllAppenders();
if (allAppenders != null) {
while (allAppenders.hasMoreElements()) {
Appender appender = (Appender) allAppenders.nextElement();
if (appender instanceof FileAppender) {
FileAppender fileAppender = (FileAppender) appender;
String rootLoggerFile = fileAppender.getFile();
rootLoggerDirectory = new File(rootLoggerFile).getParent();
break;
}
}
}
return rootLoggerDirectory;
}
use of org.apache.log4j.FileAppender in project com.revolsys.open by revolsys.
the class ScriptTool method run.
private void run() {
final long startTime = System.currentTimeMillis();
final ThreadLocalFileAppender localAppender = ThreadLocalFileAppender.getAppender();
if (localAppender != null && this.logFile != null) {
final File parentFile = this.logFile.getParentFile();
if (parentFile != null) {
parentFile.mkdirs();
}
localAppender.setLocalFile(this.logFile.getAbsolutePath());
} else if (this.logFile != null) {
final org.apache.log4j.Logger rootLogger = org.apache.log4j.Logger.getRootLogger();
try {
final Layout layout = new PatternLayout("%d\t%p\t%m%n");
final Appender appender = new FileAppender(layout, this.logFile.getAbsolutePath(), false);
rootLogger.addAppender(appender);
} catch (final IOException e) {
final Layout layout = new PatternLayout("%p\t%m%n");
final Appender appender = new ConsoleAppender(layout);
rootLogger.addAppender(appender);
Logs.error(this, "Cannot find log file " + this.logFile, e);
}
}
final StringBuilder message = new StringBuilder("Processing ");
message.append(" -s ");
message.append(this.scriptFileName);
if (this.propertiesName != null) {
message.append(" -p ");
message.append(this.propertiesName);
}
for (final Entry<String, String> parameter : this.parameters.entrySet()) {
message.append(" ");
message.append(parameter.getKey());
message.append("=");
message.append(parameter.getValue());
}
System.out.println(message);
if (System.getProperty("applicationHome") == null) {
ThreadSharedProperties.setProperty("applicationHome", ".");
System.setProperty("applicationHome", ".");
}
try {
final GenericApplicationContext beans = new GenericApplicationContext();
AnnotationConfigUtils.registerAnnotationConfigProcessors(beans, null);
beans.getBeanFactory().addPropertyEditorRegistrar(new ResourceEditorRegistrar());
if (this.scriptFile != null) {
new XmlBeanDefinitionReader(beans).loadBeanDefinitions("file:" + this.scriptFile.getAbsolutePath());
} else {
new XmlBeanDefinitionReader(beans).loadBeanDefinitions("classpath:" + this.scriptFileName);
}
beans.refresh();
try {
Logs.info(this, message.toString());
final Object bean = beans.getBean("processNetwork");
final ProcessNetwork pipeline = (ProcessNetwork) bean;
pipeline.startAndWait();
} finally {
beans.close();
}
} catch (final BeanCreationException e) {
final Throwable cause = getBeanExceptionCause(e);
Logs.error(this, cause.getMessage(), cause);
cause.printStackTrace();
System.err.flush();
}
final long endTime = System.currentTimeMillis();
final long time = endTime - startTime;
long seconds = time / 1000;
final long minutes = seconds / 60;
seconds = seconds % 60;
Logs.info(this, minutes + " minutes " + seconds + " seconds");
System.out.println(minutes + " minutes " + seconds + " seconds");
}
Aggregations