|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * This code is free software; you can redistribute it and/or modify it
|
|
24 | 24 | */
|
25 | 25 | package com.sun.org.slf4j.internal;
|
26 | 26 |
|
| 27 | +import java.security.AccessController; |
| 28 | +import java.security.PrivilegedAction; |
| 29 | +import java.util.logging.Level; |
| 30 | + |
27 | 31 | // Bridge to java.util.logging.
|
28 | 32 | public class Logger {
|
29 | 33 |
|
| 34 | + /** |
| 35 | + * StackWalker to find out the caller of this class so that it can be |
| 36 | + * shown in the log output. The multiple private log0() methods below |
| 37 | + * skip exactly 2 frames -- one log0() itself, the other one of the |
| 38 | + * public debug()/warn()/error()/trace() methods in this class -- |
| 39 | + * to find the caller. |
| 40 | + */ |
| 41 | + private static final StackWalker WALKER; |
| 42 | + static { |
| 43 | + final PrivilegedAction<StackWalker> action = |
| 44 | + () -> StackWalker.getInstance(StackWalker.Option |
| 45 | + .RETAIN_CLASS_REFERENCE); |
| 46 | + WALKER = AccessController.doPrivileged(action); |
| 47 | + } |
| 48 | + |
30 | 49 | private final java.util.logging.Logger impl;
|
31 | 50 |
|
32 | 51 | public Logger(String name) {
|
33 | 52 | impl = java.util.logging.Logger.getLogger(name);
|
34 | 53 | }
|
35 | 54 |
|
36 | 55 | public boolean isDebugEnabled() {
|
37 |
| - return impl.isLoggable(java.util.logging.Level.FINE); |
| 56 | + return impl.isLoggable(Level.FINE); |
38 | 57 | }
|
39 | 58 |
|
40 | 59 | public boolean isTraceEnabled() {
|
41 |
| - return impl.isLoggable(java.util.logging.Level.FINE); |
| 60 | + return impl.isLoggable(Level.FINE); |
42 | 61 | }
|
43 | 62 |
|
44 | 63 | public void debug(String s) {
|
45 |
| - impl.log(java.util.logging.Level.FINE, s); |
| 64 | + log0(Level.FINE, s); |
46 | 65 | }
|
47 | 66 |
|
48 | 67 | public void debug(String s, Throwable e) {
|
49 |
| - impl.log(java.util.logging.Level.FINE, s, e); |
| 68 | + log0(Level.FINE, s, e); |
50 | 69 | }
|
51 | 70 |
|
52 | 71 | public void debug(String s, Object... o) {
|
53 |
| - impl.log(java.util.logging.Level.FINE, s, o); |
| 72 | + log0(Level.FINE, s, o); |
54 | 73 | }
|
55 | 74 |
|
56 | 75 | public void trace(String s) {
|
57 |
| - impl.log(java.util.logging.Level.FINE, s); |
| 76 | + log0(Level.FINE, s); |
58 | 77 | }
|
59 | 78 |
|
60 | 79 | public void error(String s) {
|
61 |
| - impl.log(java.util.logging.Level.SEVERE, s); |
| 80 | + log0(Level.SEVERE, s); |
62 | 81 | }
|
63 | 82 |
|
64 | 83 | public void error(String s, Throwable e) {
|
65 |
| - impl.log(java.util.logging.Level.SEVERE, s, e); |
| 84 | + log0(Level.SEVERE, s, e); |
66 | 85 | }
|
67 | 86 |
|
68 | 87 | public void error(String s, Object... o) {
|
69 |
| - impl.log(java.util.logging.Level.SEVERE, s, o); |
| 88 | + log0(Level.SEVERE, s, o); |
70 | 89 | }
|
71 | 90 |
|
72 | 91 | public void warn(String s) {
|
73 |
| - impl.log(java.util.logging.Level.WARNING, s); |
| 92 | + log0(Level.WARNING, s); |
74 | 93 | }
|
75 | 94 |
|
76 | 95 | public void warn(String s, Throwable e) {
|
77 |
| - impl.log(java.util.logging.Level.WARNING, s, e); |
| 96 | + log0(Level.WARNING, s, e); |
| 97 | + } |
| 98 | + |
| 99 | + private void log0(Level level, String s) { |
| 100 | + if (impl.isLoggable(level)) { |
| 101 | + var sf = WALKER.walk(f -> f.skip(2).findFirst()).get(); |
| 102 | + impl.logp(Level.FINE, sf.getClassName(), sf.getMethodName(), s); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public void log0(Level level, String s, Throwable e) { |
| 107 | + if (impl.isLoggable(level)) { |
| 108 | + var sf = WALKER.walk(f -> f.skip(2).findFirst()).get(); |
| 109 | + impl.logp(Level.FINE, sf.getClassName(), sf.getMethodName(), s, e); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + public void log0(Level level, String s, Object... o) { |
| 114 | + if (impl.isLoggable(level)) { |
| 115 | + var sf = WALKER.walk(f -> f.skip(2).findFirst()).get(); |
| 116 | + impl.logp(Level.FINE, sf.getClassName(), sf.getMethodName(), |
| 117 | + addIndex(s), o); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Translate the log4j message format "Hello {}, {}" to the |
| 123 | + * java.util.logging format "Hello {0}, {1}". |
| 124 | + */ |
| 125 | + private static String addIndex(String s) { |
| 126 | + int start = 0; |
| 127 | + int index = 0; |
| 128 | + StringBuilder sb = new StringBuilder(); |
| 129 | + while (true) { |
| 130 | + int pos = s.indexOf("{}", start); |
| 131 | + if (pos < 0) { |
| 132 | + break; |
| 133 | + } |
| 134 | + sb.append(s, start, pos + 1).append(index++); |
| 135 | + start = pos + 1; |
| 136 | + } |
| 137 | + if (index == 0) { |
| 138 | + return s; |
| 139 | + } else { |
| 140 | + sb.append(s, start, s.length()); |
| 141 | + return sb.toString(); |
| 142 | + } |
78 | 143 | }
|
79 | 144 | }
|
0 commit comments