|
| 1 | +/* |
| 2 | + * Copyright 2010-2014, CloudBees Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.cloudbees.syslog.util; |
| 17 | + |
| 18 | +import com.cloudbees.syslog.integration.jul.util.LevelHelper; |
| 19 | + |
| 20 | +import javax.annotation.Nullable; |
| 21 | +import java.text.DateFormat; |
| 22 | +import java.text.SimpleDateFormat; |
| 23 | +import java.util.Date; |
| 24 | +import java.util.logging.Level; |
| 25 | +import java.util.logging.Logger; |
| 26 | + |
| 27 | +/** |
| 28 | + * Don't use {@link java.util.logging.Logger} as this code can be used as an Handler for java.util.logging and we would then have an infinite loop. |
| 29 | + * |
| 30 | + * @author <a href="mailto:[email protected]">Cyrille Le Clerc</a> |
| 31 | + */ |
| 32 | +public class InternalLogger { |
| 33 | + |
| 34 | + private static Level level; |
| 35 | + |
| 36 | + static { |
| 37 | + try { |
| 38 | + level = LevelHelper.findLevel(System.getProperty("com.cloudbees.syslog.debugLevel")); |
| 39 | + } catch (RuntimeException e) { |
| 40 | + e.printStackTrace(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public static InternalLogger getLogger(@Nullable String name) { |
| 45 | + return new InternalLogger(name); |
| 46 | + } |
| 47 | + |
| 48 | + public static InternalLogger getLogger(@Nullable Class clazz) { |
| 49 | + return getLogger(clazz == null ? null : clazz.getName()); |
| 50 | + } |
| 51 | + |
| 52 | + public static Level getLevel() { |
| 53 | + return level; |
| 54 | + } |
| 55 | + |
| 56 | + public static void setLevel(Level level) { |
| 57 | + InternalLogger.level = level; |
| 58 | + } |
| 59 | + |
| 60 | + private DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS"); |
| 61 | + private final String name; |
| 62 | + /** |
| 63 | + * use java.util.logger to find the logger level if not specified by system property. |
| 64 | + */ |
| 65 | + private final Logger julLogger; |
| 66 | + |
| 67 | + public InternalLogger(String name) { |
| 68 | + this.name = name; |
| 69 | + this.julLogger = Logger.getLogger(name); |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + public boolean isLoggable(Level level) { |
| 74 | + if (level == null) |
| 75 | + return false; |
| 76 | + |
| 77 | + if (this.level == null) |
| 78 | + return julLogger.isLoggable(level); |
| 79 | + |
| 80 | + return level.intValue() >= this.level.intValue(); |
| 81 | + } |
| 82 | + |
| 83 | + public void finest(@Nullable String msg) { |
| 84 | + log(Level.FINEST, msg); |
| 85 | + } |
| 86 | + |
| 87 | + public void fine(@Nullable String msg) { |
| 88 | + log(Level.FINE, msg); |
| 89 | + } |
| 90 | + |
| 91 | + public void finer(@Nullable String msg) { |
| 92 | + log(Level.FINER, msg); |
| 93 | + } |
| 94 | + |
| 95 | + public void info(@Nullable String msg) { |
| 96 | + log(Level.INFO, msg); |
| 97 | + } |
| 98 | + |
| 99 | + public void log(@Nullable Level level, @Nullable String msg) { |
| 100 | + log(level, msg, null); |
| 101 | + } |
| 102 | + |
| 103 | + public void warn(@Nullable String msg) { |
| 104 | + log(Level.WARNING, msg); |
| 105 | + } |
| 106 | + |
| 107 | + public void warn(@Nullable String msg, @Nullable Throwable t) { |
| 108 | + log(Level.WARNING, msg, t); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * synchronize for the {@link java.text.SimpleDateFormat}. |
| 113 | + * |
| 114 | + * @param level |
| 115 | + * @param msg |
| 116 | + * @param t |
| 117 | + */ |
| 118 | + public synchronized void log(@Nullable Level level, @Nullable String msg, @Nullable Throwable t) { |
| 119 | + if (!isLoggable(level)) |
| 120 | + return; |
| 121 | + System.err.println(df.format(new Date()) + " [" + Thread.currentThread().getName() + "] " + name + " - " + level.getName() + ": " + msg); |
| 122 | + if (t != null) |
| 123 | + t.printStackTrace(); |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments