diff options
Diffstat (limited to 'misc/log4j/src/main')
-rw-r--r-- | misc/log4j/src/main/java/org/onap/aaf/misc/env/log4j/LogFileNamer.java | 50 |
1 files changed, 31 insertions, 19 deletions
diff --git a/misc/log4j/src/main/java/org/onap/aaf/misc/env/log4j/LogFileNamer.java b/misc/log4j/src/main/java/org/onap/aaf/misc/env/log4j/LogFileNamer.java index 7174912f..ff7b43f0 100644 --- a/misc/log4j/src/main/java/org/onap/aaf/misc/env/log4j/LogFileNamer.java +++ b/misc/log4j/src/main/java/org/onap/aaf/misc/env/log4j/LogFileNamer.java @@ -22,56 +22,68 @@ package org.onap.aaf.misc.env.log4j; import java.io.File; +import java.io.IOException; import java.net.URL; +import java.text.SimpleDateFormat; +import java.util.Date; public class LogFileNamer { - public final String root; + private final String root; + private final String ending; + private final String dir; - public LogFileNamer(String root) { + public LogFileNamer(final String dir, final String root) { + this.dir = dir; if(root==null || "".equals(root) || root.endsWith("/")) { this.root = root; } else { this.root = root + "-"; } + ending = new SimpleDateFormat("YYYYMMdd").format(new Date()); } public LogFileNamer noPID() { return this; } + + private static final String FILE_FORMAT_STR = "%s/%s%s%s_%d.log"; /** * Accepts a String. * If Separated by "|" then first part is the Appender name, and the second is used in the FileNaming * (This is to allow for shortened Logger names, and more verbose file names) + * ONAP: jna code has license issues. Just do Date + Unique Number * * @param appender * * returns the String Appender + * @throws IOException */ - public String setAppender(String appender) { - int pipe = appender.indexOf('|'); - if(pipe>=0) { - String rv; - System.setProperty( - "LOG4J_FILENAME_"+(rv=appender.substring(0,pipe)), - root + appender.substring(pipe+1) + ".log"); - return rv; - } else { - System.setProperty( - "LOG4J_FILENAME_"+appender, - root + appender + ".log"); - return appender; - } - + public String setAppender(String appender) throws IOException { + String filename; + int i=0; + File f; + while((f=new File(filename=String.format(FILE_FORMAT_STR, dir,root, appender, ending,i))).exists()) { + ++i; + }; + f.createNewFile(); + System.setProperty( + "LOG4J_FILENAME_"+(appender), + filename); + return appender; } - public void configure(String props) { + public void configure(final String props, final String log_level) throws IOException { String fname; if(new File(fname="etc/"+props).exists()) { org.apache.log4j.PropertyConfigurator.configureAndWatch(fname,60*1000L); } else { URL rsrc = ClassLoader.getSystemResource(props); - if(rsrc==null) System.err.println("Neither File: " + fname + " or resource on Classpath " + props + " exist" ); + if(rsrc==null) { + String msg = "Neither File: " + fname + " or resource on Classpath " + props + " exist" ; + throw new IOException(msg); + } org.apache.log4j.PropertyConfigurator.configure(rsrc); } + } } |