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
27 public class LogFileNamer {
\r
28 private final String root;
\r
29 private final String dir;
\r
31 public LogFileNamer(final String dir, final String root) {
\r
33 if (root == null || "".equals(root) || root.endsWith("/")) {
\r
36 this.root = root + "-";
\r
40 public LogFileNamer noPID() {
\r
44 private static final String FIRST_FILE_FORMAT_STR = "%s/%s%s.log";
\r
45 private static final String FILE_FORMAT_STR = "%s/%s%s.%d.log";
\r
48 * Accepts a String. If Separated by "|" then first part is the Appender name,
\r
49 * and the second is used in the FileNaming (This is to allow for shortened
\r
50 * Logger names, and more verbose file names) ONAP: jna code has license issues.
\r
51 * Just do Date + Unique Number
\r
55 * returns the String Appender
\r
56 * @throws IOException
\r
58 public String setAppender(String appender) throws IOException {
\r
59 File f = new File(String.format(FIRST_FILE_FORMAT_STR, dir, root, appender));
\r
60 File lock = new File(f.getAbsoluteFile()+".lock");
\r
64 while ((f = new File(String.format(FILE_FORMAT_STR, dir, root, appender, i))).exists() &&
\r
65 (lock = new File(f.getAbsoluteFile()+".lock")).exists()) {
\r
72 lock.createNewFile();
\r
73 lock.deleteOnExit();
\r
75 } catch (IOException e) {
\r
76 throw new IOException("Cannot create file '" + f.getCanonicalPath() + '\'', e);
\r
78 System.setProperty("LOG4J_FILENAME_" + appender, f.getCanonicalPath());
\r
82 public void configure(final String path, final String fname, final String log_level) throws IOException {
\r
83 final String fullPath = path + '/' + fname;
\r
84 if (new File(fullPath).exists()) {
\r
85 org.apache.log4j.PropertyConfigurator.configureAndWatch(fullPath, 60 * 1000L);
\r
87 URL rsrc = ClassLoader.getSystemResource(fname);
\r
89 String msg = "Neither File: " + path + '/' + fname + " nor resource on Classpath " + fname + " exist";
\r
90 throw new IOException(msg);
\r
92 org.apache.log4j.PropertyConfigurator.configure(rsrc);
\r