2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.misc.env.log4j;
25 import java.io.IOException;
27 import java.text.SimpleDateFormat;
28 import java.util.Date;
30 public class LogFileNamer {
31 private final String root;
32 private final String ending;
33 private final String dir;
35 public LogFileNamer(final String dir, final String root) {
37 if(root==null || "".equals(root) || root.endsWith("/")) {
40 this.root = root + "-";
42 ending = new SimpleDateFormat("YYYYMMdd").format(new Date());
45 public LogFileNamer noPID() {
49 private static final String FILE_FORMAT_STR = "%s/%s%s%s_%d.log";
52 * If Separated by "|" then first part is the Appender name, and the second is used in the FileNaming
53 * (This is to allow for shortened Logger names, and more verbose file names)
54 * ONAP: jna code has license issues. Just do Date + Unique Number
58 * returns the String Appender
61 public String setAppender(String appender) throws IOException {
65 while((f=new File(filename=String.format(FILE_FORMAT_STR, dir,root, appender, ending,i))).exists()) {
70 "LOG4J_FILENAME_"+appender,
75 public void configure(final String path, final String fname, final String log_level) throws IOException {
76 final String fullPath=path+'/'+fname;
77 if(new File(fullPath).exists()) {
78 org.apache.log4j.PropertyConfigurator.configureAndWatch(fullPath,60*1000L);
80 URL rsrc = ClassLoader.getSystemResource(fname);
82 String msg = "Neither File: " + path + '/' + fname + " nor resource on Classpath " + fname + " exist" ;
83 throw new IOException(msg);
85 org.apache.log4j.PropertyConfigurator.configure(rsrc);