a5f118c9723af93b1e37e6602f2fbf68f6959fac
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / impl / Log4JLogTarget.java
1 /**\r
2  * ============LICENSE_START====================================================\r
3  * org.onap.aaf\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
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \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
19  *\r
20  */\r
21 \r
22 package org.onap.aaf.misc.env.impl;\r
23 \r
24 import java.io.PrintWriter;\r
25 \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
31 \r
32 /**\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
35  * \r
36  * @author Jonathan \r
37  *\r
38  */\r
39 public class Log4JLogTarget implements LogTarget {\r
40         private Level level;\r
41         private Logger log;\r
42 \r
43         public Log4JLogTarget(String loggerName, Level level) throws APIException {\r
44                 this.level = level;\r
45                 if (loggerName != null && loggerName.length() > 0) {\r
46                         log = Logger.getLogger(loggerName);\r
47                 } else {\r
48                         log = Logger.getRootLogger();\r
49                 }\r
50         }\r
51 \r
52         // @Override\r
53         public boolean isLoggable() {\r
54                 return log.isEnabledFor(level);\r
55         }\r
56 \r
57         // @Override\r
58         public void log(Object... msgs) {\r
59                 log(null, msgs);\r
60         }\r
61 \r
62         // @Override\r
63         public void log(Throwable e, Object... msgs) {\r
64                 if (log.isEnabledFor(level)) {\r
65                         StringBuilder sb = new StringBuilder();\r
66                         \r
67                         String msg;\r
68                         if (e != null) {\r
69                                 e.printStackTrace(new PrintWriter(new StringBuilderWriter(sb)));\r
70                         }\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
76                                                 if (sbl > 0) {\r
77                                                         char last = sb.charAt(sbl - 1);\r
78                                                         if (" (.".indexOf(last) < 0\r
79                                                                         && "().".indexOf(msg.charAt(0)) < 0)\r
80                                                                 sb.append(' ');\r
81                                                 }\r
82                                                 sb.append(msg);\r
83                                         }\r
84                                 }\r
85                         }\r
86                         log.log(level, sb.toString());\r
87                 }\r
88         }\r
89 \r
90         /* (non-Javadoc)\r
91          * @see com.att.inno.env.LogTarget#printf(java.lang.String, java.lang.String[])\r
92          */\r
93         @Override\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
97                 }\r
98         }\r
99 \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
107         }\r
108         \r
109 }