2 * ============LICENSE_START====================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
22 package org.onap.aaf.misc.env;
\r
24 import java.io.PrintStream;
\r
25 import java.util.Date;
\r
27 import org.onap.aaf.misc.env.util.Chrono;
\r
30 * LogTarget is the interface with which to assign any kind of Logging Implementations.
\r
32 * Implement for any Logging Library of your choice, and for any logging string Format desired.
\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
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
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
53 public void log(Throwable t, Object ... msgs) {
\r
56 public boolean isLoggable() {
\r
61 public void printf(String fmt, Object ... vars) {
\r
65 // A Convenient LogTarget to write to the Console
\r
66 public static final LogTarget SYSOUT = new StreamLogTarget(System.out,false);
\r
68 // A Convenient LogTarget to write to the Console
\r
69 public static final LogTarget SYSERR = new StreamLogTarget(System.err,false);
\r
71 public static class StreamLogTarget implements LogTarget {
\r
72 private final PrintStream out;
\r
73 private final boolean closeMe;
\r
75 public StreamLogTarget(PrintStream ps) {
\r
79 /* Do NOT close SYSTEM ERR or OUT*/
\r
80 protected StreamLogTarget(PrintStream ps, boolean shouldClose) {
\r
82 closeMe = shouldClose;
\r
84 public void log(Object ... msgs) {
\r
85 out.print(Chrono.dateFmt.format(new Date()));
\r
87 for (Object str : msgs) {
\r
89 out.print(str.toString());
\r
98 public void log(Throwable t, Object ... msgs) {
\r
99 out.print(Chrono.dateFmt.format(new Date()));
\r
101 for (Object str : msgs) {
\r
102 out.print(str.toString());
\r
106 t.printStackTrace(out);
\r
110 public boolean isLoggable() {
\r
115 public void printf(String fmt, Object ... vars) {
\r
116 log(String.format(fmt,vars));
\r
119 public void close() {
\r