1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package loggerfinder;
import java.lang.*;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
public class SimpleLoggerFinder extends System.LoggerFinder {
public static final CopyOnWriteArrayList<Object> LOGS = new CopyOnWriteArrayList<>();
static {
try {
long sleep = new Random().nextLong(1000L) + 1L;
// simulate a slow load service
Thread.sleep(sleep);
System.getLogger("dummy")
.log(System.Logger.Level.INFO,
"Logger finder service load sleep value: " + sleep);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
private final Map<String, SimpleLogger> loggers = new ConcurrentHashMap<>();
public SimpleLoggerFinder() {
System.getLogger("dummy")
.log(System.Logger.Level.INFO,
"Logger finder service created");
}
@Override
public System.Logger getLogger(String name, Module module) {
return loggers.computeIfAbsent(name, SimpleLogger::new);
}
private static class SimpleLogger implements System.Logger {
private final java.util.logging.Logger logger;
private static final class SimpleHandler extends Handler {
@Override
public void publish(LogRecord record) {
LOGS.add(record);
}
@Override public void flush() { }
@Override public void close() { }
}
public SimpleLogger(String name) {
logger = Logger.getLogger(name);
logger.addHandler(new SimpleHandler());
}
@Override
public String getName() {
return logger.getName();
}
java.util.logging.Level level(Level level) {
return switch (level) {
case ALL -> java.util.logging.Level.ALL;
case DEBUG -> java.util.logging.Level.FINE;
case TRACE -> java.util.logging.Level.FINER;
case INFO -> java.util.logging.Level.INFO;
case WARNING -> java.util.logging.Level.WARNING;
case ERROR -> java.util.logging.Level.SEVERE;
case OFF -> java.util.logging.Level.OFF;
};
}
@Override
public boolean isLoggable(Level level) {
return logger.isLoggable(level(level));
}
@Override
public void log(Level level, ResourceBundle bundle, String msg, Throwable thrown) {
var julLevel = level(level);
if (!logger.isLoggable(julLevel)) return;
if (bundle != null) {
logger.logrb(julLevel, bundle, msg, thrown);
} else {
logger.log(julLevel, msg, thrown);
}
}
@Override
public void log(Level level, ResourceBundle bundle, String format, Object... params) {
var julLevel = level(level);
if (!logger.isLoggable(julLevel)) return;
if (params == null) {
if (bundle == null) {
logger.log(julLevel, format);
} else {
logger.logrb(julLevel, bundle, format);
}
} else {
if (bundle == null) {
logger.log(julLevel, format, params);
} else {
logger.logrb(julLevel, bundle, format, params);
}
}
}
}
}
|