001/** 002 * Copyright (C) 2006-2025 Talend Inc. - www.talend.com 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.talend.sdk.component.slf4j; 017 018import static java.util.Locale.ROOT; 019 020import java.io.PrintStream; 021 022import org.slf4j.helpers.MarkerIgnoringBase; 023import org.slf4j.helpers.MessageFormatter; 024 025import lombok.Getter; 026 027public class StdLogger extends MarkerIgnoringBase { 028 029 @Getter 030 private final String name; 031 032 private final boolean trace; 033 034 private final boolean debug; 035 036 private final boolean info; 037 038 private final boolean warn; 039 040 private final boolean error; 041 042 StdLogger(final String name) { 043 this.name = name; 044 045 final String level = System.getProperty(getClass().getName() + ".level", "info"); 046 switch (level.toLowerCase(ROOT)) { 047 case "trace": 048 trace = debug = info = warn = error = true; 049 break; 050 case "debug": 051 trace = false; 052 debug = info = warn = error = true; 053 break; 054 case "info": 055 trace = debug = false; 056 info = warn = error = true; 057 break; 058 case "warn": 059 trace = debug = info = false; 060 warn = error = true; 061 break; 062 case "error": 063 trace = debug = info = warn = false; 064 error = true; 065 break; 066 default: 067 trace = debug = false; 068 info = warn = error = true; 069 } 070 } 071 072 @Override 073 public boolean isTraceEnabled() { 074 return trace; 075 } 076 077 @Override 078 public void trace(final String msg) { 079 if (!trace) { 080 return; 081 } 082 log("TRACE", msg, null, System.out); 083 } 084 085 @Override 086 public void trace(final String format, final Object arg) { 087 if (!trace) { 088 return; 089 } 090 log("TRACE", MessageFormatter.format(format, arg).getMessage(), null, System.out); 091 } 092 093 @Override 094 public void trace(final String format, final Object arg1, final Object arg2) { 095 if (!trace) { 096 return; 097 } 098 log("TRACE", MessageFormatter.format(format, arg1, arg1).getMessage(), null, System.out); 099 } 100 101 @Override 102 public void trace(final String format, final Object... arguments) { 103 if (!trace) { 104 return; 105 } 106 log("TRACE", MessageFormatter.arrayFormat(format, arguments).getMessage(), null, System.out); 107 } 108 109 @Override 110 public void trace(final String msg, final Throwable throwable) { 111 if (!trace) { 112 return; 113 } 114 log("TRACE", msg, throwable, System.out); 115 } 116 117 @Override 118 public boolean isDebugEnabled() { 119 return debug; 120 } 121 122 @Override 123 public void debug(final String msg) { 124 if (!debug) { 125 return; 126 } 127 log("DEBUG", msg, null, System.out); 128 } 129 130 @Override 131 public void debug(final String format, final Object arg) { 132 if (!debug) { 133 return; 134 } 135 log("DEBUG", MessageFormatter.format(format, arg).getMessage(), null, System.out); 136 } 137 138 @Override 139 public void debug(final String format, final Object arg1, final Object arg2) { 140 if (!debug) { 141 return; 142 } 143 log("DEBUG", MessageFormatter.format(format, arg1, arg1).getMessage(), null, System.out); 144 } 145 146 @Override 147 public void debug(final String format, final Object... arguments) { 148 if (!debug) { 149 return; 150 } 151 log("DEBUG", MessageFormatter.arrayFormat(format, arguments).getMessage(), null, System.out); 152 } 153 154 @Override 155 public void debug(final String msg, final Throwable throwable) { 156 if (!debug) { 157 return; 158 } 159 log("DEBUG", msg, throwable, System.out); 160 } 161 162 @Override 163 public boolean isInfoEnabled() { 164 return info; 165 } 166 167 @Override 168 public void info(final String msg) { 169 if (!info) { 170 return; 171 } 172 log("INFO", msg, null, System.out); 173 } 174 175 @Override 176 public void info(final String format, final Object arg) { 177 if (!info) { 178 return; 179 } 180 log("INFO", MessageFormatter.format(format, arg).getMessage(), null, System.out); 181 } 182 183 @Override 184 public void info(final String format, final Object arg1, final Object arg2) { 185 if (!info) { 186 return; 187 } 188 log("INFO", MessageFormatter.format(format, arg1, arg1).getMessage(), null, System.out); 189 } 190 191 @Override 192 public void info(final String format, final Object... arguments) { 193 if (!info) { 194 return; 195 } 196 log("INFO", MessageFormatter.arrayFormat(format, arguments).getMessage(), null, System.out); 197 } 198 199 @Override 200 public void info(final String msg, final Throwable throwable) { 201 if (!info) { 202 return; 203 } 204 log("INFO", msg, throwable, System.out); 205 } 206 207 @Override 208 public boolean isWarnEnabled() { 209 return warn; 210 } 211 212 @Override 213 public void warn(final String msg) { 214 if (!warn) { 215 return; 216 } 217 log("WARN", msg, null, System.out); 218 } 219 220 @Override 221 public void warn(final String format, final Object arg) { 222 if (!warn) { 223 return; 224 } 225 log("WARN", MessageFormatter.format(format, arg).getMessage(), null, System.out); 226 } 227 228 @Override 229 public void warn(final String format, final Object arg1, final Object arg2) { 230 if (!warn) { 231 return; 232 } 233 log("WARN", MessageFormatter.format(format, arg1, arg1).getMessage(), null, System.out); 234 } 235 236 @Override 237 public void warn(final String format, final Object... arguments) { 238 if (!warn) { 239 return; 240 } 241 log("WARN", MessageFormatter.arrayFormat(format, arguments).getMessage(), null, System.out); 242 } 243 244 @Override 245 public void warn(final String msg, final Throwable throwable) { 246 if (!warn) { 247 return; 248 } 249 log("WARN", msg, throwable, System.out); 250 } 251 252 @Override 253 public boolean isErrorEnabled() { 254 return error; 255 } 256 257 @Override 258 public void error(final String msg) { 259 if (!error) { 260 return; 261 } 262 log("ERROR", msg, null, System.err); 263 } 264 265 @Override 266 public void error(final String format, final Object arg) { 267 if (!error) { 268 return; 269 } 270 log("ERROR", MessageFormatter.format(format, arg).getMessage(), null, System.err); 271 } 272 273 @Override 274 public void error(final String format, final Object arg1, final Object arg2) { 275 if (!error) { 276 return; 277 } 278 log("ERROR", MessageFormatter.format(format, arg1, arg1).getMessage(), null, System.err); 279 } 280 281 @Override 282 public void error(final String format, final Object... arguments) { 283 if (!error) { 284 return; 285 } 286 log("ERROR", MessageFormatter.arrayFormat(format, arguments).getMessage(), null, System.err); 287 } 288 289 @Override 290 public void error(final String msg, final Throwable throwable) { 291 if (!error) { 292 return; 293 } 294 log("ERROR", msg, throwable, System.err); 295 } 296 297 private void log(final String level, final String message, final Throwable throwable, final PrintStream out) { 298 final StringBuilder builder = new StringBuilder(message.length() + level.length() + 3) 299 .append('[') 300 .append(level) 301 .append("] ") 302 .append(message); 303 out.println(builder); 304 if (throwable != null) { 305 throwable.printStackTrace(out); 306 } 307 out.flush(); 308 } 309}