AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / impl / Log4JLogTarget.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.misc.env.impl;
23
24 import java.io.PrintWriter;
25
26 import org.apache.log4j.Level;
27 import org.apache.log4j.Logger;
28 import org.onap.aaf.misc.env.APIException;
29 import org.onap.aaf.misc.env.LogTarget;
30 import org.onap.aaf.misc.env.util.StringBuilderWriter;
31
32 /**
33  * Many services have chosen to use Log4J for their lower level Logging Implementation.  This LogTarget will allow
34  * any of the messages sent to be set to the appropriate Log4J level.
35  * 
36  * @author Jonathan 
37  *
38  */
39 public class Log4JLogTarget implements LogTarget {
40         private Level level;
41         private Logger log;
42
43         public Log4JLogTarget(String loggerName, Level level) throws APIException {
44                 this.level = level;
45                 if (loggerName != null && loggerName.length() > 0) {
46                         log = Logger.getLogger(loggerName);
47                 } else {
48                         log = Logger.getRootLogger();
49                 }
50         }
51
52         // @Override
53         public boolean isLoggable() {
54                 return log.isEnabledFor(level);
55         }
56
57         // @Override
58         public void log(Object... msgs) {
59                 log(null, msgs);
60         }
61
62         // @Override
63         public void log(Throwable e, Object... msgs) {
64                 if (log.isEnabledFor(level)) {
65                         StringBuilder sb = new StringBuilder();
66                         
67                         String msg;
68                         if (e != null) {
69                                 e.printStackTrace(new PrintWriter(new StringBuilderWriter(sb)));
70                         }
71                         for (int i = 0; i < msgs.length; ++i) {
72                                 if(msgs[i]!=null) {
73                                         msg = msgs[i].toString();
74                                         if (msg != null && msg.length() > 0) {
75                                                 int sbl = sb.length();
76                                                 if (sbl > 0) {
77                                                         char last = sb.charAt(sbl - 1);
78                                                         if (" (.".indexOf(last) < 0
79                                                                         && "().".indexOf(msg.charAt(0)) < 0)
80                                                                 sb.append(' ');
81                                                 }
82                                                 sb.append(msg);
83                                         }
84                                 }
85                         }
86                         log.log(level, sb.toString());
87                 }
88         }
89
90         /* (non-Javadoc)
91          * @see com.att.inno.env.LogTarget#printf(java.lang.String, java.lang.String[])
92          */
93         @Override
94         public void printf(String fmt, Object ... vars) {
95                 if(log.isEnabledFor(level)) {
96                         log.log(level,String.format(fmt,vars));
97                 }
98         }
99
100         public static void setLog4JEnv(String loggerName, BasicEnv env) throws APIException {
101                         env.fatal = new Log4JLogTarget(loggerName,Level.FATAL);
102                         env.error = new Log4JLogTarget(loggerName,Level.ERROR);
103                         env.warn = env.audit = env.init = new Log4JLogTarget(loggerName,Level.WARN);
104                         env.info = new Log4JLogTarget(loggerName,Level.INFO);
105                         env.debug = new Log4JLogTarget(loggerName,Level.DEBUG);
106                         env.trace = new Log4JLogTarget(loggerName,Level.TRACE);
107         }
108         
109 }