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