aboutsummaryrefslogtreecommitdiff
path: root/src/java.base/share/classes/jdk/internal/logger/SimpleConsoleLogger.java
blob: 769e8d2a1b658e68af33e4da274a176f34cf44b9 (plain) (blame)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
 * Copyright (c) 2015, 2021, 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.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * 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 jdk.internal.logger;

import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.StackWalker.StackFrame;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.time.ZonedDateTime;
import java.util.Optional;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.function.Function;
import java.lang.System.Logger;
import java.util.function.Predicate;
import java.util.function.Supplier;
import sun.security.action.GetPropertyAction;
import sun.util.logging.PlatformLogger;
import sun.util.logging.PlatformLogger.ConfigurableBridge.LoggerConfiguration;

/**
 * A simple console logger to emulate the behavior of JUL loggers when
 * in the default configuration. SimpleConsoleLoggers are also used when
 * JUL is not present and no DefaultLoggerFinder is installed.
 */
public class SimpleConsoleLogger extends LoggerConfiguration
    implements Logger, PlatformLogger.Bridge, PlatformLogger.ConfigurableBridge {

    static final Level DEFAULT_LEVEL = getDefaultLevel();
    static final PlatformLogger.Level DEFAULT_PLATFORM_LEVEL =
            PlatformLogger.toPlatformLevel(DEFAULT_LEVEL);

    static Level getDefaultLevel() {
        String levelName = GetPropertyAction
                .privilegedGetProperty("jdk.system.logger.level", "INFO");
        try {
            return Level.valueOf(levelName);
        } catch (IllegalArgumentException iae) {
            return Level.INFO;
        }
    }

    final String name;
    volatile PlatformLogger.Level  level;
    final boolean usePlatformLevel;
    SimpleConsoleLogger(String name, boolean usePlatformLevel) {
        this.name = name;
        this.usePlatformLevel = usePlatformLevel;
    }

    String getSimpleFormatString() {
        return Formatting.SIMPLE_CONSOLE_LOGGER_FORMAT;
    }

    PlatformLogger.Level defaultPlatformLevel() {
        return DEFAULT_PLATFORM_LEVEL;
    }

    @Override
    public final String getName() {
        return name;
    }

    private Enum<?> logLevel(PlatformLogger.Level level) {
        return usePlatformLevel ? level : level.systemLevel();
    }

    private Enum<?> logLevel(Level level) {
        return usePlatformLevel ? PlatformLogger.toPlatformLevel(level) : level;
    }

    // ---------------------------------------------------
    //                 From Logger
    // ---------------------------------------------------

    @Override
    public final boolean isLoggable(Level level) {
        return isLoggable(PlatformLogger.toPlatformLevel(level));
    }

    @Override
    public final void log(Level level, ResourceBundle bundle, String key, Throwable thrown) {
        if (isLoggable(level)) {
            if (bundle != null) {
                key = getString(bundle, key);
            }
            publish(getCallerInfo(), logLevel(level), key, thrown);
        }
    }

    @Override
    public final void log(Level level, ResourceBundle bundle, String format, Object... params) {
        if (isLoggable(level)) {
            if (bundle != null) {
                format = getString(bundle, format);
            }
            publish(getCallerInfo(), logLevel(level), format, params);
        }
    }

    // ---------------------------------------------------
    //             From PlatformLogger.Bridge
    // ---------------------------------------------------

    @Override
    public final boolean isLoggable(PlatformLogger.Level level) {
        final PlatformLogger.Level effectiveLevel =  effectiveLevel();
        return level != PlatformLogger.Level.OFF
                && level.ordinal() >= effectiveLevel.ordinal();
    }

    @Override
    public final boolean isEnabled() {
        return level != PlatformLogger.Level.OFF;
    }

    @Override
    public final void log(PlatformLogger.Level level, String msg) {
        if (isLoggable(level)) {
            publish(getCallerInfo(), logLevel(level), msg);
        }
    }

    @Override
    public final void log(PlatformLogger.Level level, String msg, Throwable thrown) {
        if (isLoggable(level)) {
            publish(getCallerInfo(), logLevel(level), msg, thrown);
        }
    }

    @Override
    public final void log(PlatformLogger.Level level, String msg, Object... params) {
        if (isLoggable(level)) {
            publish(getCallerInfo(), logLevel(level), msg, params);
        }
    }

    private PlatformLogger.Level effectiveLevel() {
        if (level == null) return defaultPlatformLevel();
        return level;
    }

    @Override
    public final PlatformLogger.Level getPlatformLevel() {
        return level;
    }

    @Override
    public final void setPlatformLevel(PlatformLogger.Level newLevel) {
        level = newLevel;
    }

    @Override
    public final LoggerConfiguration getLoggerConfiguration() {
        return this;
    }

    /**
     * Default platform logging support - output messages to System.err -
     * equivalent to ConsoleHandler with SimpleFormatter.
     */
    static PrintStream outputStream() {
        return System.err;
    }

    // Returns the caller's class and method's name; best effort
    // if cannot infer, return the logger's name.
    private String getCallerInfo() {
        Optional<StackWalker.StackFrame> frame = new CallerFinder().get();
        if (frame.isPresent()) {
            return frame.get().getClassName() + " " + frame.get().getMethodName();
        } else {
            return name;
        }
    }

    /*
     * CallerFinder is a stateful predicate.
     */
    @SuppressWarnings("removal")
    static final class CallerFinder implements Predicate<StackWalker.StackFrame> {
        private static final StackWalker WALKER;
        static {
            final PrivilegedAction<StackWalker> action = new PrivilegedAction<>() {
                @Override
                public StackWalker run() {
                    return StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
                }
            };
            WALKER = AccessController.doPrivileged(action);
        }

        /**
         * Returns StackFrame of the caller's frame.
         * @return StackFrame of the caller's frame.
         */
        Optional<StackWalker.StackFrame> get() {
            return WALKER.walk((s) -> s.filter(this).findFirst());
        }

        private boolean lookingForLogger = true;
        /**
         * Returns true if we have found the caller's frame, false if the frame
         * must be skipped.
         *
         * @param t The frame info.
         * @return true if we have found the caller's frame, false if the frame
         * must be skipped.
         */
        @Override
        public boolean test(StackWalker.StackFrame t) {
            final String cname = t.getClassName();
            // We should skip all frames until we have found the logger,
            // because these frames could be frames introduced by e.g. custom
            // sub classes of Handler.
            if (lookingForLogger) {
                // Skip all frames until we have found the first logger frame.
                lookingForLogger = !isLoggerImplFrame(cname);
                return false;
            }
            // Continue walking until we've found the relevant calling frame.
            // Skips logging/logger infrastructure.
            return !Formatting.isFilteredFrame(t);
        }

        private boolean isLoggerImplFrame(String cname) {
            return (cname.equals("sun.util.logging.PlatformLogger") ||
                    cname.equals("jdk.internal.logger.SimpleConsoleLogger"));
        }
    }

    private String getCallerInfo(String sourceClassName, String sourceMethodName) {
        if (sourceClassName == null) return name;
        if (sourceMethodName == null) return sourceClassName;
        return sourceClassName + " " + sourceMethodName;
    }

    private String toString(Throwable thrown) {
        String throwable = "";
        if (thrown != null) {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            pw.println();
            thrown.printStackTrace(pw);
            pw.close();
            throwable = sw.toString();
        }
        return throwable;
    }

    private synchronized String format(Enum<?> level,
            String msg, Throwable thrown, String callerInfo) {

        ZonedDateTime zdt = ZonedDateTime.now();
        String throwable = toString(thrown);

        return String.format(getSimpleFormatString(),
                         zdt,
                         callerInfo,
                         name,
                         level.name(),
                         msg,
                         throwable);
    }

    // publish accepts both PlatformLogger Levels and LoggerFinder Levels.
    private void publish(String callerInfo, Enum<?> level, String msg) {
        outputStream().print(format(level, msg, null, callerInfo));
    }
    // publish accepts both PlatformLogger Levels and LoggerFinder Levels.
    private void publish(String callerInfo, Enum<?> level, String msg, Throwable thrown) {
        outputStream().print(format(level, msg, thrown, callerInfo));
    }
    // publish accepts both PlatformLogger Levels and LoggerFinder Levels.
    private void publish(String callerInfo, Enum<?> level, String msg, Object... params) {
        msg = params == null || params.length == 0 ? msg
                : Formatting.formatMessage(msg, params);
        outputStream().print(format(level, msg, null, callerInfo));
    }

    public static SimpleConsoleLogger makeSimpleLogger(String name) {
        return new SimpleConsoleLogger(name, false);
    }

    @Override
    public final void log(PlatformLogger.Level level, Supplier<String> msgSupplier) {
        if (isLoggable(level)) {
            publish(getCallerInfo(), logLevel(level), msgSupplier.get());
        }
    }

    @Override
    public final void log(PlatformLogger.Level level, Throwable thrown,
            Supplier<String> msgSupplier) {
        if (isLoggable(level)) {
            publish(getCallerInfo(), logLevel(level), msgSupplier.get(), thrown);
        }
    }

    @Override
    public final void logp(PlatformLogger.Level level, String sourceClass,
            String sourceMethod, String msg) {
        if (isLoggable(level)) {
            publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msg);
        }
    }

    @Override
    public final void logp(PlatformLogger.Level level, String sourceClass,
            String sourceMethod, Supplier<String> msgSupplier) {
        if (isLoggable(level)) {
            publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msgSupplier.get());
        }
    }

    @Override
    public final void logp(PlatformLogger.Level level, String sourceClass, String sourceMethod,
            String msg, Object... params) {
        if (isLoggable(level)) {
            publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msg, params);
        }
    }

    @Override
    public final void logp(PlatformLogger.Level level, String sourceClass,
            String sourceMethod, String msg, Throwable thrown) {
        if (isLoggable(level)) {
            publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msg, thrown);
        }
    }

    @Override
    public final void logp(PlatformLogger.Level level, String sourceClass,
            String sourceMethod, Throwable thrown, Supplier<String> msgSupplier) {
        if (isLoggable(level)) {
            publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msgSupplier.get(), thrown);
        }
    }

    @Override
    public final void logrb(PlatformLogger.Level level, String sourceClass,
            String sourceMethod, ResourceBundle bundle, String key, Object... params) {
        if (isLoggable(level)) {
            String msg = bundle == null ? key : getString(bundle, key);
            publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msg, params);
        }
    }

    @Override
    public final void logrb(PlatformLogger.Level level, String sourceClass,
            String sourceMethod, ResourceBundle bundle, String key, Throwable thrown) {
        if (isLoggable(level)) {
            String msg = bundle == null ? key : getString(bundle, key);
            publish(getCallerInfo(sourceClass, sourceMethod), logLevel(level), msg, thrown);
        }
    }

    @Override
    public final void logrb(PlatformLogger.Level level, ResourceBundle bundle,
            String key, Object... params) {
        if (isLoggable(level)) {
            String msg = bundle == null ? key : getString(bundle,key);
            publish(getCallerInfo(), logLevel(level), msg, params);
        }
    }

    @Override
    public final void logrb(PlatformLogger.Level level, ResourceBundle bundle,
            String key, Throwable thrown) {
        if (isLoggable(level)) {
            String msg = bundle == null ? key : getString(bundle,key);
            publish(getCallerInfo(), logLevel(level), msg, thrown);
        }
    }

    static String getString(ResourceBundle bundle, String key) {
        if (bundle == null || key == null) return key;
        try {
            return bundle.getString(key);
        } catch (MissingResourceException x) {
            // Emulate what java.util.logging Formatters do
            // We don't want unchecked exception to propagate up to
            // the caller's code.
            return key;
        }
    }

    static final class Formatting {
        // The default simple log format string.
        // Used both by SimpleConsoleLogger when java.logging is not present,
        // and by SurrogateLogger and java.util.logging.SimpleFormatter when
        // java.logging is present.
        static final String DEFAULT_FORMAT =
            "%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n";

        // The system property key that allows to change the default log format
        // when java.logging is not present. This is used to control the formatting
        // of the SimpleConsoleLogger.
        static final String DEFAULT_FORMAT_PROP_KEY =
            "jdk.system.logger.format";

        // The system property key that allows to change the default log format
        // when java.logging is present. This is used to control the formatting
        // of the SurrogateLogger (used before java.util.logging.LogManager is
        // initialized) and the java.util.logging.SimpleFormatter (used after
        // java.util.logging.LogManager is  initialized).
        static final String JUL_FORMAT_PROP_KEY =
            "java.util.logging.SimpleFormatter.format";

        // The simple console logger format string
        static final String SIMPLE_CONSOLE_LOGGER_FORMAT =
                getSimpleFormat(DEFAULT_FORMAT_PROP_KEY, null);

        // Make it easier to wrap Logger...
        static private final String[] skips;
        static {
            String additionalPkgs =
                    GetPropertyAction.privilegedGetProperty("jdk.logger.packages");
            skips = additionalPkgs == null ? new String[0] : additionalPkgs.split(",");
        }

        static boolean isFilteredFrame(StackFrame st) {
            // skip logging/logger infrastructure
            if (System.Logger.class.isAssignableFrom(st.getDeclaringClass())) {
                return true;
            }

            // fast escape path: all the prefixes below start with 's' or 'j' and
            // have more than 12 characters.
            final String cname = st.getClassName();
            char c = cname.length() < 12 ? 0 : cname.charAt(0);
            if (c == 's') {
                // skip internal machinery classes
                if (cname.startsWith("sun.util.logging."))   return true;
                if (cname.startsWith("sun.rmi.runtime.Log")) return true;
            } else if (c == 'j') {
                // Message delayed at Bootstrap: no need to go further up.
                if (cname.startsWith("jdk.internal.logger.BootstrapLogger$LogEvent")) return false;
                // skip public machinery classes
                if (cname.startsWith("jdk.internal.logger."))          return true;
                if (cname.startsWith("java.util.logging."))            return true;
                if (cname.startsWith("java.lang.invoke.MethodHandle")) return true;
                if (cname.startsWith("java.security.AccessController")) return true;
            }

            // check additional prefixes if any are specified.
            if (skips.length > 0) {
                for (int i=0; i<skips.length; i++) {
                    if (!skips[i].isEmpty() && cname.startsWith(skips[i])) {
                        return true;
                    }
                }
            }

            return false;
        }

        static String getSimpleFormat(String key, Function<String, String> defaultPropertyGetter) {
            // Double check that 'key' is one of the expected property names:
            // - DEFAULT_FORMAT_PROP_KEY is used to control the
            //   SimpleConsoleLogger format when java.logging is
            //   not present.
            // - JUL_FORMAT_PROP_KEY is used when this method is called
            //   from the SurrogateLogger subclass. It is used to control the
            //   SurrogateLogger format and java.util.logging.SimpleFormatter
            //   format when java.logging is present.
            // This method should not be called with any other key.
            if (!DEFAULT_FORMAT_PROP_KEY.equals(key)
                    && !JUL_FORMAT_PROP_KEY.equals(key)) {
                throw new IllegalArgumentException("Invalid property name: " + key);
            }

            // Do not use any lambda in this method. Using a lambda here causes
            //    jdk/test/java/lang/invoke/lambda/LogGeneratedClassesTest.java
            // to fail - because that test has a testcase which somehow references
            // PlatformLogger and counts the number of generated lambda classes.
            String format = GetPropertyAction.privilegedGetProperty(key);

            if (format == null && defaultPropertyGetter != null) {
                format = defaultPropertyGetter.apply(key);
            }
            if (format != null) {
                try {
                    // validate the user-defined format string
                    String.format(format, ZonedDateTime.now(), "", "", "", "", "");
                } catch (IllegalArgumentException e) {
                    // illegal syntax; fall back to the default format
                    format = DEFAULT_FORMAT;
                }
            } else {
                format = DEFAULT_FORMAT;
            }
            return format;
        }


        // Copied from java.util.logging.Formatter.formatMessage
        static String formatMessage(String format, Object... parameters) {
            // Do the formatting.
            try {
                if (parameters == null || parameters.length == 0) {
                    // No parameters.  Just return format string.
                    return format;
                }
                // Is it a java.text style format?
                // Ideally we could match with
                // Pattern.compile("\\{\\d").matcher(format).find())
                // However the cost is 14% higher, so we cheaply check for
                //
                boolean isJavaTestFormat = false;
                final int len = format.length();
                for (int i=0; i<len-2; i++) {
                    final char c = format.charAt(i);
                    if (c == '{') {
                        final int d = format.charAt(i+1);
                        if (d >= '0' && d <= '9') {
                            isJavaTestFormat = true;
                            break;
                        }
                    }
                }
                if (isJavaTestFormat) {
                    return java.text.MessageFormat.format(format, parameters);
                }
                return format;
            } catch (Exception ex) {
                // Formatting failed: use format string.
                return format;
            }
        }
    }
}