import java.io.File;\r
import java.io.IOException;\r
import java.net.URL;\r
-import java.text.SimpleDateFormat;\r
-import java.util.Date;\r
\r
public class LogFileNamer {\r
private final String root;\r
- private final String ending;\r
private final String dir;\r
\r
public LogFileNamer(final String dir, final String root) {\r
} else {\r
this.root = root + "-";\r
}\r
- ending = new SimpleDateFormat("YYYYMMdd").format(new Date());\r
}\r
\r
public LogFileNamer noPID() {\r
return this;\r
}\r
\r
- private static final String FILE_FORMAT_STR = "%s/%s%s%s_%d.log";\r
+ private static final String FIRST_FILE_FORMAT_STR = "%s/%s%s.log";\r
+ private static final String FILE_FORMAT_STR = "%s/%s%s.%d.log";\r
\r
/**\r
* Accepts a String. If Separated by "|" then first part is the Appender name,\r
* @throws IOException\r
*/\r
public String setAppender(String appender) throws IOException {\r
- int i = 0;\r
- File f;\r
- while ((f = new File(String.format(FILE_FORMAT_STR, dir, root, appender, ending, i))).exists()) {\r
- ++i;\r
- }\r
+ File f = new File(String.format(FIRST_FILE_FORMAT_STR, dir, root, appender));\r
+ if(f.exists()) {\r
+ int i = 0;\r
+ while ((f = new File(String.format(FILE_FORMAT_STR, dir, root, appender, i))).exists()) {\r
+ ++i;\r
+ }\r
+ }\r
\r
- f.createNewFile();\r
+ try {\r
+ f.createNewFile();\r
+ } catch (IOException e) {\r
+ throw new IOException("Cannot create file '" + f.getCanonicalPath() + '\'', e);\r
+ }\r
System.setProperty("LOG4J_FILENAME_" + appender, f.getCanonicalPath());\r
return appender;\r
}\r