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
22 package org.onap.aaf.misc.env.impl;
\r
24 import java.io.PrintWriter;
\r
26 import org.apache.log4j.Level;
\r
27 import org.apache.log4j.Logger;
\r
28 import org.onap.aaf.misc.env.APIException;
\r
29 import org.onap.aaf.misc.env.LogTarget;
\r
30 import org.onap.aaf.misc.env.util.StringBuilderWriter;
\r
33 * Many services have chosen to use Log4J for their lower level Logging Implementation. This LogTarget will allow
\r
34 * any of the messages sent to be set to the appropriate Log4J level.
\r
39 public class Log4JLogTarget implements LogTarget {
\r
40 private Level level;
\r
43 public Log4JLogTarget(String loggerName, Level level) throws APIException {
\r
45 if (loggerName != null && loggerName.length() > 0) {
\r
46 log = Logger.getLogger(loggerName);
\r
48 log = Logger.getRootLogger();
\r
53 public boolean isLoggable() {
\r
54 return log.isEnabledFor(level);
\r
58 public void log(Object... msgs) {
\r
63 public void log(Throwable e, Object... msgs) {
\r
64 if (log.isEnabledFor (level)) {
\r
65 StringBuilder sb = new StringBuilder();
\r
69 e.printStackTrace(new PrintWriter(new StringBuilderWriter(sb)));
\r
71 for (int i = 0; i < msgs.length; ++i) {
\r
72 if (msgs[i]!=null) {
\r
73 msg = msgs[i].toString();
\r
74 if (msg != null && msg.length() > 0) {
\r
75 int sbl = sb.length();
\r
77 char last = sb.charAt(sbl - 1);
\r
78 if (" (.".indexOf(last) < 0
\r
79 && "().".indexOf(msg.charAt(0)) < 0)
\r
86 log.log(level, sb.toString());
\r
91 * @see com.att.inno.env.LogTarget#printf(java.lang.String, java.lang.String[])
\r
94 public void printf(String fmt, Object ... vars) {
\r
95 if (log.isEnabledFor (level)) {
\r
96 log.log(level,String.format(fmt,vars));
\r
100 public static void setLog4JEnv(String loggerName, BasicEnv env) throws APIException {
\r
101 env.fatal = new Log4JLogTarget(loggerName,Level.FATAL);
\r
102 env.error = new Log4JLogTarget(loggerName,Level.ERROR);
\r
103 env.warn = env.audit = env.init = new Log4JLogTarget(loggerName,Level.WARN);
\r
104 env.info = new Log4JLogTarget(loggerName,Level.INFO);
\r
105 env.debug = new Log4JLogTarget(loggerName,Level.DEBUG);
\r
106 env.trace = new Log4JLogTarget(loggerName,Level.TRACE);
\r