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