1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one *
3 * or more contributor license agreements. See the NOTICE file *
4 * distributed with this work for additional information *
5 * regarding copyright ownership. The ASF licenses this file *
6 * to you under the Apache License, Version 2.0 (the *
7 * "License"); you may not use this file except in compliance *
8 * with the License. You may obtain a copy of the License at *
9 * *
10 * http://www.apache.org/licenses/LICENSE-2.0 *
11 * *
12 * Unless required by applicable law or agreed to in writing, *
13 * software distributed under the License is distributed on an *
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15 * KIND, either express or implied. See the License for the *
16 * specific language governing permissions and limitations *
17 * under the License. *
18 */
19 package org.apache.rat.utils;
20
21 import org.apache.rat.api.EnvVar;
22
23 /**
24 * A default implementation of Log that writes to {@code System.out} and {@code System.err}.
25 */
26 public final class DefaultLog implements Log {
27 /**
28 * The instance of the default log.
29 */
30 private static Log instance = new DefaultLog();
31
32 /**
33 * Retrieves the DefaultLog instance.
34 * @return the Default log instance.
35 */
36 public static Log getInstance() {
37 return instance;
38 }
39
40 /**
41 * Sets the default log instance.
42 * If not set an instance of DefaultLog will be returned
43 * @param newInstance a Log to use as the default.
44 * @return the old instance.
45 */
46 public static Log setInstance(final Log newInstance) {
47 Log result = instance;
48 instance = newInstance == null ? new DefaultLog() : newInstance;
49 return result;
50 }
51
52 /**
53 * Creates a new instance of the default log.
54 * @return A new instance of the default log.
55 */
56 public static Log createDefault() {
57 return new DefaultLog();
58 }
59
60 /** The level at which we will write messages */
61 private Level level;
62
63 private DefaultLog() {
64 try {
65 level = EnvVar.RAT_DEFAULT_LOG_LEVEL.isSet() ?
66 Level.valueOf(EnvVar.RAT_DEFAULT_LOG_LEVEL.getValue().toUpperCase()) : Level.INFO;
67 } catch (IllegalArgumentException e) {
68 level = Level.INFO;
69 log(Level.WARN, "Invalid log level set in environment: " + EnvVar.RAT_DEFAULT_LOG_LEVEL.getValue().toUpperCase(), e);
70 }
71 }
72
73 /**
74 * Sets the level. Log messages below the specified level will
75 * not be written to the log.
76 * @param level the level to use when writing messages.
77 */
78 public void setLevel(final Level level) {
79 this.level = level;
80 }
81
82 /**
83 * Gets the level we are writing at.
84 * @return the level we are writing at.
85 */
86 public Level getLevel() {
87 return level;
88 }
89
90 @Override
91 public void log(final Level level, final String msg) {
92 if (isEnabled(level)) {
93 switch (level) {
94 case DEBUG:
95 case INFO:
96 case WARN:
97 System.out.format("%s: %s%n", level, msg);
98 break;
99 case ERROR:
100 System.err.format("%s: %s%n", level, msg);
101 break;
102 case OFF:
103 default:
104 break;
105 }
106 }
107 }
108 }