AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / LogTarget.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;
23
24 import java.io.PrintStream;
25 import java.util.Date;
26
27 import org.onap.aaf.misc.env.util.Chrono;
28
29 /**
30  * LogTarget is the interface with which to assign any kind of Logging Implementations.
31  * 
32  * Implement for any Logging Library of your choice, and for any logging string Format desired.
33  * 
34  * Included are several Static Implementations for various uses:
35  *       NULL: Does nothing with Logging Messages
36  *   SYSOUT: Writes messages in general form to System Out
37  *   SYSERR: Writes messages in general form to System Err
38  *   
39  * @author Jonathan
40  *
41  */
42 public interface LogTarget {
43         public abstract void log(Object... msgs);
44         public abstract void log(Throwable e, Object ... msgs);
45         public abstract boolean isLoggable();
46         public abstract void printf(String fmt, Object ... vars);
47
48         // A Convenient LogTarget to insert when a NO-OP is desired.
49         public static final LogTarget NULL = new LogTarget() {
50                 public void log(Object ... msgs) {
51                 }
52
53                 public void log(Throwable t, Object ... msgs) {
54                 }
55
56                 public boolean isLoggable() {
57                         return false;
58                 }
59
60                 @Override
61                 public void printf(String fmt, Object ... vars) {
62                 }
63         };
64
65         // A Convenient LogTarget to write to the Console
66         public static final LogTarget SYSOUT = new LogTarget() {
67                 public void log(Object ... msgs) {
68                         PrintStream out = System.out;
69                         out.print(org.onap.aaf.misc.env.util.Chrono.dateFmt.format(new Date()));
70                         out.print(": ");
71                         for(Object str : msgs) {
72                                 if(str!=null) {
73                                         out.print(str.toString());
74                                         out.print(' ');
75                                 } else {
76                                         out.print("null ");
77                                 }
78                         }
79                         out.println();
80                 }
81
82                 public void log(Throwable t, Object ... msgs) {
83                         PrintStream out = System.out;
84                         out.print(Chrono.dateFmt.format(new Date()));
85                         out.print(": ");
86                         for(Object str : msgs) {
87                                 out.print(str.toString());
88                                 out.print(' ');
89                         }
90                         out.println();
91                         t.printStackTrace(out);
92                         out.println();
93                 }
94
95                 public boolean isLoggable() {
96                         return true;
97                 }
98
99                 @Override
100                 public void printf(String fmt, Object ... vars) {
101                         log(String.format(fmt,vars));
102                 }
103         };
104         
105         // A Convenient LogTarget to write to the Console
106         public static final LogTarget SYSERR = new LogTarget() {
107                 public void log(Object ... msgs) {
108                         PrintStream out = System.err;
109                         out.print(Chrono.dateFmt.format(new Date()));
110                         out.print(": ");
111                         for(Object str : msgs) {
112                                 out.print(str.toString());
113                                 out.print(' ');
114                         }
115                         out.println();
116                         out.flush();
117                 }
118
119                 public void log(Throwable t, Object ... msgs) {
120                         PrintStream out = System.err;
121                         out.print(Chrono.dateFmt.format(new Date()));
122                         out.print(": ");
123                         for(Object str : msgs) {
124                                 out.print(str.toString());
125                                 out.print(' ');
126                         }
127                         out.println();
128                         t.printStackTrace(out);
129                 }
130
131                 public boolean isLoggable() {
132                         return true;
133                 }
134                 @Override
135                 public void printf(String fmt, Object ... vars) {
136                         log(String.format(fmt,vars));
137                 }
138
139         };
140
141
142 };