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