ac3e8b45da14c57bb29423a7069fcb64c97b23b8
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / impl / JavaUtilLogTarget.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.util.logging.Level;\r
25 import java.util.logging.Logger;\r
26 \r
27 import org.onap.aaf.misc.env.LogTarget;\r
28 \r
29 /**\r
30  * This LogTarget Implementation is included mostly because the JavaUtil based logging is included in the\r
31  * JDK.  This makes the default implementation independent of any external Jars.\r
32  * \r
33  *  Log4j is often considered more Enterprise capable.  See Log4JLogTarget for that implementation\r
34  * \r
35  * @author Jonathan\r
36  *\r
37  */\r
38 public class JavaUtilLogTarget implements LogTarget {\r
39         private Level level;\r
40         private Logger log;\r
41 \r
42         public JavaUtilLogTarget(Logger logger, Level theLevel) {\r
43                 log = logger;\r
44                 level = theLevel;\r
45         }\r
46 \r
47         public boolean isLoggable() {\r
48                 return log.isLoggable(level);\r
49         }\r
50 \r
51         public void log(Object ... msgs) {\r
52                 if(log.isLoggable(level)) {\r
53                         StringBuilder sb = new StringBuilder();\r
54                         String msg;\r
55                         for(int i=0;i<msgs.length;++i) {\r
56                                 msg = msgs[i].toString();\r
57                                 if(msg!=null && msg.length()>0) {\r
58                                         int sbl = sb.length();\r
59                                         if(sbl>0) {\r
60                                                 char last = sb.charAt(sbl-1);\r
61                                                 if(" (.".indexOf(last)<0 && "().".indexOf(msg.charAt(0))<0)sb.append(' ');\r
62                                         }\r
63                                         sb.append(msg);\r
64                                 }\r
65                         }\r
66                         log.log(level, sb.toString());\r
67                 }\r
68         }\r
69 \r
70         public void log(Throwable e, Object ... msgs) {\r
71                 String str = e.getLocalizedMessage();\r
72                 if(str==null) {\r
73                         str = e.getMessage();\r
74                 }\r
75                 if(str==null) {\r
76                         str = e.getClass().getName();\r
77                 }\r
78                 log.log(level,str,msgs);\r
79         }\r
80 \r
81         /* (non-Javadoc)\r
82          * @see com.att.inno.env.LogTarget#printf(java.lang.String, java.lang.String[])\r
83          */\r
84         @Override\r
85         public void printf(String fmt, Object ... vars) {\r
86                 if(log.isLoggable(level)) {\r
87                         log.log(level,String.format(fmt,vars));\r
88                 }\r
89         }\r
90 }       \r