author | ihse |
Mon, 19 Feb 2018 23:25:11 +0100 | |
branch | ihse-cflags-rewrite-branch |
changeset 56155 | f98e22c513dd |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
43197 | 2 |
* Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util.logging; |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
27 |
import java.time.Instant; |
2 | 28 |
import java.util.*; |
2632
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
29 |
import java.util.concurrent.atomic.AtomicInteger; |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
30 |
import java.util.concurrent.atomic.AtomicLong; |
2 | 31 |
import java.io.*; |
34723
734a1c90ce86
8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents:
34439
diff
changeset
|
32 |
import java.security.AccessController; |
734a1c90ce86
8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents:
34439
diff
changeset
|
33 |
import java.security.PrivilegedAction; |
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
34 |
import java.time.Clock; |
34372 | 35 |
import java.util.function.Predicate; |
36679
0e6911cee995
8150840: Add an internal system property to control the default level of System.Logger when java.logging is not present.
dfuchs
parents:
35302
diff
changeset
|
36 |
import static jdk.internal.logger.SurrogateLogger.isFilteredFrame; |
2947
b0135c99348e
6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents:
2632
diff
changeset
|
37 |
|
2 | 38 |
/** |
39 |
* LogRecord objects are used to pass logging requests between |
|
40 |
* the logging framework and individual log Handlers. |
|
41 |
* <p> |
|
42 |
* When a LogRecord is passed into the logging framework it |
|
43 |
* logically belongs to the framework and should no longer be |
|
44 |
* used or updated by the client application. |
|
45 |
* <p> |
|
46 |
* Note that if the client application has not specified an |
|
47 |
* explicit source method name and source class name, then the |
|
48 |
* LogRecord class will infer them automatically when they are |
|
49 |
* first accessed (due to a call on getSourceMethodName or |
|
50 |
* getSourceClassName) by analyzing the call stack. Therefore, |
|
51 |
* if a logging Handler wants to pass off a LogRecord to another |
|
52 |
* thread, or to transmit it over RMI, and if it wishes to subsequently |
|
53 |
* obtain method name or class name information it should call |
|
54 |
* one of getSourceClassName or getSourceMethodName to force |
|
55 |
* the values to be filled in. |
|
56 |
* <p> |
|
57 |
* <b> Serialization notes:</b> |
|
58 |
* <ul> |
|
59 |
* <li>The LogRecord class is serializable. |
|
60 |
* |
|
61 |
* <li> Because objects in the parameters array may not be serializable, |
|
62 |
* during serialization all objects in the parameters array are |
|
63 |
* written as the corresponding Strings (using Object.toString). |
|
64 |
* |
|
65 |
* <li> The ResourceBundle is not transmitted as part of the serialized |
|
66 |
* form, but the resource bundle name is, and the recipient object's |
|
67 |
* readObject method will attempt to locate a suitable resource bundle. |
|
68 |
* |
|
69 |
* </ul> |
|
70 |
* |
|
71 |
* @since 1.4 |
|
72 |
*/ |
|
73 |
||
74 |
public class LogRecord implements java.io.Serializable { |
|
2632
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
75 |
private static final AtomicLong globalSequenceNumber |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
76 |
= new AtomicLong(0); |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
77 |
|
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
78 |
/** |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
79 |
* The default value of threadID will be the current thread's |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
80 |
* thread id, for ease of correlation, unless it is greater than |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
81 |
* MIN_SEQUENTIAL_THREAD_ID, in which case we try harder to keep |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
82 |
* our promise to keep threadIDs unique by avoiding collisions due |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
83 |
* to 32-bit wraparound. Unfortunately, LogRecord.getThreadID() |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
84 |
* returns int, while Thread.getId() returns long. |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
85 |
*/ |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
86 |
private static final int MIN_SEQUENTIAL_THREAD_ID = Integer.MAX_VALUE / 2; |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
87 |
|
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
88 |
private static final AtomicInteger nextThreadId |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
89 |
= new AtomicInteger(MIN_SEQUENTIAL_THREAD_ID); |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
90 |
|
7803
56bc97d69d93
6880112: Project Coin: Port JDK core library code to use diamond operator
smarks
parents:
7026
diff
changeset
|
91 |
private static final ThreadLocal<Integer> threadIds = new ThreadLocal<>(); |
2 | 92 |
|
93 |
/** |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
94 |
* Logging message level |
2 | 95 |
*/ |
96 |
private Level level; |
|
97 |
||
98 |
/** |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
99 |
* Sequence number |
2 | 100 |
*/ |
101 |
private long sequenceNumber; |
|
102 |
||
103 |
/** |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
104 |
* Class that issued logging call |
2 | 105 |
*/ |
106 |
private String sourceClassName; |
|
107 |
||
108 |
/** |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
109 |
* Method that issued logging call |
2 | 110 |
*/ |
111 |
private String sourceMethodName; |
|
112 |
||
113 |
/** |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
114 |
* Non-localized raw message text |
2 | 115 |
*/ |
116 |
private String message; |
|
117 |
||
118 |
/** |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
119 |
* Thread ID for thread that issued logging call. |
2 | 120 |
*/ |
121 |
private int threadID; |
|
122 |
||
123 |
/** |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
124 |
* The Throwable (if any) associated with log message |
2 | 125 |
*/ |
126 |
private Throwable thrown; |
|
127 |
||
128 |
/** |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
129 |
* Name of the source Logger. |
2 | 130 |
*/ |
131 |
private String loggerName; |
|
132 |
||
133 |
/** |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
134 |
* Resource bundle name to localized log message. |
2 | 135 |
*/ |
136 |
private String resourceBundleName; |
|
137 |
||
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
138 |
/** |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
139 |
* Event time. |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
34723
diff
changeset
|
140 |
* @since 9 |
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
141 |
*/ |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
142 |
private Instant instant; |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
143 |
|
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
144 |
/** |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
145 |
* @serialField level Level Logging message level |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
146 |
* @serialField sequenceNumber long Sequence number |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
147 |
* @serialField sourceClassName String Class that issued logging call |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
148 |
* @serialField sourceMethodName String Method that issued logging call |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
149 |
* @serialField message String Non-localized raw message text |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
150 |
* @serialField threadID int Thread ID for thread that issued logging call |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
151 |
* @serialField millis long Truncated event time in milliseconds since 1970 |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
152 |
* - calculated as getInstant().toEpochMilli(). |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
153 |
* The event time instant can be reconstructed using |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
154 |
* <code>Instant.ofEpochSecond(millis/1000, (millis % 1000) * 1000_000 + nanoAdjustment)</code> |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
155 |
* @serialField nanoAdjustment int Nanoseconds adjustment to the millisecond of |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
156 |
* event time - calculated as getInstant().getNano() % 1000_000 |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
157 |
* The event time instant can be reconstructed using |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
158 |
* <code>Instant.ofEpochSecond(millis/1000, (millis % 1000) * 1000_000 + nanoAdjustment)</code> |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
159 |
* <p> |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
34723
diff
changeset
|
160 |
* Since: 9 |
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
161 |
* @serialField thrown Throwable The Throwable (if any) associated with log |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
162 |
* message |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
163 |
* @serialField loggerName String Name of the source Logger |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
164 |
* @serialField resourceBundleName String Resource bundle name to localized |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
165 |
* log message |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
166 |
*/ |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
167 |
private static final ObjectStreamField[] serialPersistentFields = |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
168 |
new ObjectStreamField[] { |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
169 |
new ObjectStreamField("level", Level.class), |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
170 |
new ObjectStreamField("sequenceNumber", long.class), |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
171 |
new ObjectStreamField("sourceClassName", String.class), |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
172 |
new ObjectStreamField("sourceMethodName", String.class), |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
173 |
new ObjectStreamField("message", String.class), |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
174 |
new ObjectStreamField("threadID", int.class), |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
175 |
new ObjectStreamField("millis", long.class), |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
176 |
new ObjectStreamField("nanoAdjustment", int.class), |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
177 |
new ObjectStreamField("thrown", Throwable.class), |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
178 |
new ObjectStreamField("loggerName", String.class), |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
179 |
new ObjectStreamField("resourceBundleName", String.class), |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
180 |
}; |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
181 |
|
2 | 182 |
private transient boolean needToInferCaller; |
183 |
private transient Object parameters[]; |
|
184 |
private transient ResourceBundle resourceBundle; |
|
185 |
||
186 |
/** |
|
2632
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
187 |
* Returns the default value for a new LogRecord's threadID. |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
188 |
*/ |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
189 |
private int defaultThreadID() { |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
190 |
long tid = Thread.currentThread().getId(); |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
191 |
if (tid < MIN_SEQUENTIAL_THREAD_ID) { |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
192 |
return (int) tid; |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
193 |
} else { |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
194 |
Integer id = threadIds.get(); |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
195 |
if (id == null) { |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
196 |
id = nextThreadId.getAndIncrement(); |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
197 |
threadIds.set(id); |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
198 |
} |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
199 |
return id; |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
200 |
} |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
201 |
} |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
202 |
|
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
203 |
/** |
2 | 204 |
* Construct a LogRecord with the given level and message values. |
205 |
* <p> |
|
206 |
* The sequence property will be initialized with a new unique value. |
|
207 |
* These sequence values are allocated in increasing order within a VM. |
|
208 |
* <p> |
|
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
34723
diff
changeset
|
209 |
* Since JDK 9, the event time is represented by an {@link Instant}. |
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
210 |
* The instant property will be initialized to the {@linkplain |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
211 |
* Instant#now() current instant}, using the best available |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
212 |
* {@linkplain Clock#systemUTC() clock} on the system. |
2 | 213 |
* <p> |
214 |
* The thread ID property will be initialized with a unique ID for |
|
215 |
* the current thread. |
|
216 |
* <p> |
|
217 |
* All other properties will be initialized to "null". |
|
218 |
* |
|
219 |
* @param level a logging level value |
|
220 |
* @param msg the raw non-localized logging message (may be null) |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
221 |
* @see java.time.Clock#systemUTC() |
2 | 222 |
*/ |
223 |
public LogRecord(Level level, String msg) { |
|
29094
a4fd2b5e49f8
8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents:
27080
diff
changeset
|
224 |
this.level = Objects.requireNonNull(level); |
2 | 225 |
message = msg; |
226 |
// Assign a thread ID and a unique sequence number. |
|
2632
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
227 |
sequenceNumber = globalSequenceNumber.getAndIncrement(); |
9779b9cbae42
6278014: java.util.logging.LogRecord.getThreadID() should provide real thread id
martin
parents:
2
diff
changeset
|
228 |
threadID = defaultThreadID(); |
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
229 |
instant = Instant.now(); |
2 | 230 |
needToInferCaller = true; |
231 |
} |
|
232 |
||
233 |
/** |
|
3853 | 234 |
* Get the source Logger's name. |
2 | 235 |
* |
236 |
* @return source logger name (may be null) |
|
237 |
*/ |
|
238 |
public String getLoggerName() { |
|
239 |
return loggerName; |
|
240 |
} |
|
241 |
||
242 |
/** |
|
3853 | 243 |
* Set the source Logger's name. |
2 | 244 |
* |
245 |
* @param name the source logger name (may be null) |
|
246 |
*/ |
|
247 |
public void setLoggerName(String name) { |
|
248 |
loggerName = name; |
|
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
* Get the localization resource bundle |
|
253 |
* <p> |
|
254 |
* This is the ResourceBundle that should be used to localize |
|
255 |
* the message string before formatting it. The result may |
|
256 |
* be null if the message is not localizable, or if no suitable |
|
257 |
* ResourceBundle is available. |
|
18565 | 258 |
* @return the localization resource bundle |
2 | 259 |
*/ |
260 |
public ResourceBundle getResourceBundle() { |
|
261 |
return resourceBundle; |
|
262 |
} |
|
263 |
||
264 |
/** |
|
265 |
* Set the localization resource bundle. |
|
266 |
* |
|
267 |
* @param bundle localization bundle (may be null) |
|
268 |
*/ |
|
269 |
public void setResourceBundle(ResourceBundle bundle) { |
|
270 |
resourceBundle = bundle; |
|
271 |
} |
|
272 |
||
273 |
/** |
|
274 |
* Get the localization resource bundle name |
|
275 |
* <p> |
|
276 |
* This is the name for the ResourceBundle that should be |
|
277 |
* used to localize the message string before formatting it. |
|
278 |
* The result may be null if the message is not localizable. |
|
18565 | 279 |
* @return the localization resource bundle name |
2 | 280 |
*/ |
281 |
public String getResourceBundleName() { |
|
282 |
return resourceBundleName; |
|
283 |
} |
|
284 |
||
285 |
/** |
|
286 |
* Set the localization resource bundle name. |
|
287 |
* |
|
288 |
* @param name localization bundle name (may be null) |
|
289 |
*/ |
|
290 |
public void setResourceBundleName(String name) { |
|
291 |
resourceBundleName = name; |
|
292 |
} |
|
293 |
||
294 |
/** |
|
295 |
* Get the logging message level, for example Level.SEVERE. |
|
296 |
* @return the logging message level |
|
297 |
*/ |
|
298 |
public Level getLevel() { |
|
299 |
return level; |
|
300 |
} |
|
301 |
||
302 |
/** |
|
303 |
* Set the logging message level, for example Level.SEVERE. |
|
304 |
* @param level the logging message level |
|
305 |
*/ |
|
306 |
public void setLevel(Level level) { |
|
307 |
if (level == null) { |
|
308 |
throw new NullPointerException(); |
|
309 |
} |
|
310 |
this.level = level; |
|
311 |
} |
|
312 |
||
313 |
/** |
|
314 |
* Get the sequence number. |
|
315 |
* <p> |
|
316 |
* Sequence numbers are normally assigned in the LogRecord |
|
317 |
* constructor, which assigns unique sequence numbers to |
|
318 |
* each new LogRecord in increasing order. |
|
319 |
* @return the sequence number |
|
320 |
*/ |
|
321 |
public long getSequenceNumber() { |
|
322 |
return sequenceNumber; |
|
323 |
} |
|
324 |
||
325 |
/** |
|
326 |
* Set the sequence number. |
|
327 |
* <p> |
|
328 |
* Sequence numbers are normally assigned in the LogRecord constructor, |
|
329 |
* so it should not normally be necessary to use this method. |
|
18565 | 330 |
* @param seq the sequence number |
2 | 331 |
*/ |
332 |
public void setSequenceNumber(long seq) { |
|
333 |
sequenceNumber = seq; |
|
334 |
} |
|
335 |
||
336 |
/** |
|
337 |
* Get the name of the class that (allegedly) issued the logging request. |
|
338 |
* <p> |
|
339 |
* Note that this sourceClassName is not verified and may be spoofed. |
|
340 |
* This information may either have been provided as part of the |
|
341 |
* logging call, or it may have been inferred automatically by the |
|
342 |
* logging framework. In the latter case, the information may only |
|
343 |
* be approximate and may in fact describe an earlier call on the |
|
344 |
* stack frame. |
|
345 |
* <p> |
|
346 |
* May be null if no information could be obtained. |
|
347 |
* |
|
348 |
* @return the source class name |
|
349 |
*/ |
|
350 |
public String getSourceClassName() { |
|
351 |
if (needToInferCaller) { |
|
352 |
inferCaller(); |
|
353 |
} |
|
354 |
return sourceClassName; |
|
355 |
} |
|
356 |
||
357 |
/** |
|
358 |
* Set the name of the class that (allegedly) issued the logging request. |
|
359 |
* |
|
360 |
* @param sourceClassName the source class name (may be null) |
|
361 |
*/ |
|
362 |
public void setSourceClassName(String sourceClassName) { |
|
363 |
this.sourceClassName = sourceClassName; |
|
364 |
needToInferCaller = false; |
|
365 |
} |
|
366 |
||
367 |
/** |
|
368 |
* Get the name of the method that (allegedly) issued the logging request. |
|
369 |
* <p> |
|
370 |
* Note that this sourceMethodName is not verified and may be spoofed. |
|
371 |
* This information may either have been provided as part of the |
|
372 |
* logging call, or it may have been inferred automatically by the |
|
373 |
* logging framework. In the latter case, the information may only |
|
374 |
* be approximate and may in fact describe an earlier call on the |
|
375 |
* stack frame. |
|
376 |
* <p> |
|
377 |
* May be null if no information could be obtained. |
|
378 |
* |
|
379 |
* @return the source method name |
|
380 |
*/ |
|
381 |
public String getSourceMethodName() { |
|
382 |
if (needToInferCaller) { |
|
383 |
inferCaller(); |
|
384 |
} |
|
385 |
return sourceMethodName; |
|
386 |
} |
|
387 |
||
388 |
/** |
|
389 |
* Set the name of the method that (allegedly) issued the logging request. |
|
390 |
* |
|
391 |
* @param sourceMethodName the source method name (may be null) |
|
392 |
*/ |
|
393 |
public void setSourceMethodName(String sourceMethodName) { |
|
394 |
this.sourceMethodName = sourceMethodName; |
|
395 |
needToInferCaller = false; |
|
396 |
} |
|
397 |
||
398 |
/** |
|
399 |
* Get the "raw" log message, before localization or formatting. |
|
400 |
* <p> |
|
401 |
* May be null, which is equivalent to the empty string "". |
|
402 |
* <p> |
|
403 |
* This message may be either the final text or a localization key. |
|
404 |
* <p> |
|
405 |
* During formatting, if the source logger has a localization |
|
406 |
* ResourceBundle and if that ResourceBundle has an entry for |
|
407 |
* this message string, then the message string is replaced |
|
408 |
* with the localized value. |
|
409 |
* |
|
410 |
* @return the raw message string |
|
411 |
*/ |
|
412 |
public String getMessage() { |
|
413 |
return message; |
|
414 |
} |
|
415 |
||
416 |
/** |
|
417 |
* Set the "raw" log message, before localization or formatting. |
|
418 |
* |
|
419 |
* @param message the raw message string (may be null) |
|
420 |
*/ |
|
421 |
public void setMessage(String message) { |
|
422 |
this.message = message; |
|
423 |
} |
|
424 |
||
425 |
/** |
|
426 |
* Get the parameters to the log message. |
|
427 |
* |
|
428 |
* @return the log message parameters. May be null if |
|
429 |
* there are no parameters. |
|
430 |
*/ |
|
431 |
public Object[] getParameters() { |
|
432 |
return parameters; |
|
433 |
} |
|
434 |
||
435 |
/** |
|
436 |
* Set the parameters to the log message. |
|
437 |
* |
|
438 |
* @param parameters the log message parameters. (may be null) |
|
439 |
*/ |
|
440 |
public void setParameters(Object parameters[]) { |
|
441 |
this.parameters = parameters; |
|
442 |
} |
|
443 |
||
444 |
/** |
|
445 |
* Get an identifier for the thread where the message originated. |
|
446 |
* <p> |
|
447 |
* This is a thread identifier within the Java VM and may or |
|
448 |
* may not map to any operating system ID. |
|
449 |
* |
|
450 |
* @return thread ID |
|
451 |
*/ |
|
452 |
public int getThreadID() { |
|
453 |
return threadID; |
|
454 |
} |
|
455 |
||
456 |
/** |
|
457 |
* Set an identifier for the thread where the message originated. |
|
458 |
* @param threadID the thread ID |
|
459 |
*/ |
|
460 |
public void setThreadID(int threadID) { |
|
461 |
this.threadID = threadID; |
|
462 |
} |
|
463 |
||
464 |
/** |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
465 |
* Get truncated event time in milliseconds since 1970. |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
466 |
* |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
467 |
* @return truncated event time in millis since 1970 |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
468 |
* |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
469 |
* @implSpec This is equivalent to calling |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
470 |
* {@link #getInstant() getInstant().toEpochMilli()}. |
2 | 471 |
* |
34439
fbb6e8d9611c
8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents:
34372
diff
changeset
|
472 |
* @apiNote To get the full nanosecond resolution event time, |
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
473 |
* use {@link #getInstant()}. |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
474 |
* |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
475 |
* @see #getInstant() |
2 | 476 |
*/ |
477 |
public long getMillis() { |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
478 |
return instant.toEpochMilli(); |
2 | 479 |
} |
480 |
||
481 |
/** |
|
482 |
* Set event time. |
|
483 |
* |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
484 |
* @param millis event time in millis since 1970. |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
485 |
* |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
486 |
* @implSpec This is equivalent to calling |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
487 |
* {@link #setInstant(java.time.Instant) |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
488 |
* setInstant(Instant.ofEpochMilli(millis))}. |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
489 |
* |
34439
fbb6e8d9611c
8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents:
34372
diff
changeset
|
490 |
* @deprecated LogRecord maintains timestamps with nanosecond resolution, |
fbb6e8d9611c
8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents:
34372
diff
changeset
|
491 |
* using {@link Instant} values. For this reason, |
fbb6e8d9611c
8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents:
34372
diff
changeset
|
492 |
* {@link #setInstant(java.time.Instant) setInstant()} |
fbb6e8d9611c
8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents:
34372
diff
changeset
|
493 |
* should be used in preference to {@code setMillis()}. |
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
494 |
* |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
495 |
* @see #setInstant(java.time.Instant) |
2 | 496 |
*/ |
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
497 |
@Deprecated |
2 | 498 |
public void setMillis(long millis) { |
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
499 |
this.instant = Instant.ofEpochMilli(millis); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
500 |
} |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
501 |
|
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
502 |
/** |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
503 |
* Gets the instant that the event occurred. |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
504 |
* |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
505 |
* @return the instant that the event occurred. |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
506 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
34723
diff
changeset
|
507 |
* @since 9 |
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
508 |
*/ |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
509 |
public Instant getInstant() { |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
510 |
return instant; |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
511 |
} |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
512 |
|
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
513 |
/** |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
514 |
* Sets the instant that the event occurred. |
34439
fbb6e8d9611c
8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents:
34372
diff
changeset
|
515 |
* <p> |
fbb6e8d9611c
8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents:
34372
diff
changeset
|
516 |
* If the given {@code instant} represents a point on the time-line too |
fbb6e8d9611c
8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents:
34372
diff
changeset
|
517 |
* far in the future or past to fit in a {@code long} milliseconds and |
fbb6e8d9611c
8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents:
34372
diff
changeset
|
518 |
* nanoseconds adjustment, then an {@code ArithmeticException} will be |
fbb6e8d9611c
8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents:
34372
diff
changeset
|
519 |
* thrown. |
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
520 |
* |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
521 |
* @param instant the instant that the event occurred. |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
522 |
* |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
523 |
* @throws NullPointerException if {@code instant} is null. |
34439
fbb6e8d9611c
8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents:
34372
diff
changeset
|
524 |
* @throws ArithmeticException if numeric overflow would occur while |
fbb6e8d9611c
8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents:
34372
diff
changeset
|
525 |
* calling {@link Instant#toEpochMilli() instant.toEpochMilli()}. |
fbb6e8d9611c
8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents:
34372
diff
changeset
|
526 |
* |
35302
e4d2275861c3
8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents:
34723
diff
changeset
|
527 |
* @since 9 |
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
528 |
*/ |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
529 |
public void setInstant(Instant instant) { |
34439
fbb6e8d9611c
8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents:
34372
diff
changeset
|
530 |
instant.toEpochMilli(); |
fbb6e8d9611c
8144262: LogRecord.getMillis() method is a convenience API that should not have been deprecated
dfuchs
parents:
34372
diff
changeset
|
531 |
this.instant = instant; |
2 | 532 |
} |
533 |
||
534 |
/** |
|
535 |
* Get any throwable associated with the log record. |
|
536 |
* <p> |
|
537 |
* If the event involved an exception, this will be the |
|
538 |
* exception object. Otherwise null. |
|
539 |
* |
|
540 |
* @return a throwable |
|
541 |
*/ |
|
542 |
public Throwable getThrown() { |
|
543 |
return thrown; |
|
544 |
} |
|
545 |
||
546 |
/** |
|
547 |
* Set a throwable associated with the log event. |
|
548 |
* |
|
549 |
* @param thrown a throwable (may be null) |
|
550 |
*/ |
|
551 |
public void setThrown(Throwable thrown) { |
|
552 |
this.thrown = thrown; |
|
553 |
} |
|
554 |
||
555 |
private static final long serialVersionUID = 5372048053134512534L; |
|
556 |
||
557 |
/** |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
558 |
* @serialData Serialized fields, followed by a two byte version number |
2 | 559 |
* (major byte, followed by minor byte), followed by information on |
560 |
* the log record parameter array. If there is no parameter array, |
|
561 |
* then -1 is written. If there is a parameter array (possible of zero |
|
562 |
* length) then the array length is written as an integer, followed |
|
563 |
* by String values for each parameter. If a parameter is null, then |
|
564 |
* a null String is written. Otherwise the output of Object.toString() |
|
565 |
* is written. |
|
566 |
*/ |
|
567 |
private void writeObject(ObjectOutputStream out) throws IOException { |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
568 |
// We have to write serialized fields first. |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
569 |
ObjectOutputStream.PutField pf = out.putFields(); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
570 |
pf.put("level", level); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
571 |
pf.put("sequenceNumber", sequenceNumber); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
572 |
pf.put("sourceClassName", sourceClassName); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
573 |
pf.put("sourceMethodName", sourceMethodName); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
574 |
pf.put("message", message); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
575 |
pf.put("threadID", threadID); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
576 |
pf.put("millis", instant.toEpochMilli()); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
577 |
pf.put("nanoAdjustment", instant.getNano() % 1000_000); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
578 |
pf.put("thrown", thrown); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
579 |
pf.put("loggerName", loggerName); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
580 |
pf.put("resourceBundleName", resourceBundleName); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
581 |
out.writeFields(); |
2 | 582 |
|
583 |
// Write our version number. |
|
584 |
out.writeByte(1); |
|
585 |
out.writeByte(0); |
|
586 |
if (parameters == null) { |
|
587 |
out.writeInt(-1); |
|
588 |
return; |
|
589 |
} |
|
590 |
out.writeInt(parameters.length); |
|
591 |
// Write string values for the parameters. |
|
22078
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
18565
diff
changeset
|
592 |
for (Object parameter : parameters) { |
bdec5d53e98c
8030851: Update code in java.util to use newer language features
psandoz
parents:
18565
diff
changeset
|
593 |
out.writeObject(Objects.toString(parameter, null)); |
2 | 594 |
} |
595 |
} |
|
596 |
||
597 |
private void readObject(ObjectInputStream in) |
|
598 |
throws IOException, ClassNotFoundException { |
|
29117
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
599 |
// We have to read serialized fields first. |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
600 |
ObjectInputStream.GetField gf = in.readFields(); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
601 |
level = (Level) gf.get("level", null); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
602 |
sequenceNumber = gf.get("sequenceNumber", 0L); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
603 |
sourceClassName = (String) gf.get("sourceClassName", null); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
604 |
sourceMethodName = (String) gf.get("sourceMethodName", null); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
605 |
message = (String) gf.get("message", null); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
606 |
threadID = gf.get("threadID", 0); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
607 |
long millis = gf.get("millis", 0L); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
608 |
int nanoOfMilli = gf.get("nanoAdjustment", 0); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
609 |
instant = Instant.ofEpochSecond( |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
610 |
millis / 1000L, (millis % 1000L) * 1000_000L + nanoOfMilli); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
611 |
thrown = (Throwable) gf.get("thrown", null); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
612 |
loggerName = (String) gf.get("loggerName", null); |
7956b5dc0eac
8072645: java.util.logging should use java.time to get more precise time stamps
dfuchs
parents:
29094
diff
changeset
|
613 |
resourceBundleName = (String) gf.get("resourceBundleName", null); |
2 | 614 |
|
615 |
// Read version number. |
|
616 |
byte major = in.readByte(); |
|
617 |
byte minor = in.readByte(); |
|
618 |
if (major != 1) { |
|
619 |
throw new IOException("LogRecord: bad version: " + major + "." + minor); |
|
620 |
} |
|
621 |
int len = in.readInt(); |
|
43197 | 622 |
if (len < -1) { |
623 |
throw new NegativeArraySizeException(); |
|
624 |
} else if (len == -1) { |
|
2 | 625 |
parameters = null; |
43197 | 626 |
} else if (len < 255) { |
2 | 627 |
parameters = new Object[len]; |
628 |
for (int i = 0; i < parameters.length; i++) { |
|
629 |
parameters[i] = in.readObject(); |
|
630 |
} |
|
43197 | 631 |
} else { |
632 |
List<Object> params = new ArrayList<>(Math.min(len, 1024)); |
|
633 |
for (int i = 0; i < len; i++) { |
|
634 |
params.add(in.readObject()); |
|
635 |
} |
|
636 |
parameters = params.toArray(new Object[params.size()]); |
|
2 | 637 |
} |
638 |
// If necessary, try to regenerate the resource bundle. |
|
639 |
if (resourceBundleName != null) { |
|
640 |
try { |
|
27080 | 641 |
// use system class loader to ensure the ResourceBundle |
642 |
// instance is a different instance than null loader uses |
|
643 |
final ResourceBundle bundle = |
|
644 |
ResourceBundle.getBundle(resourceBundleName, |
|
645 |
Locale.getDefault(), |
|
646 |
ClassLoader.getSystemClassLoader()); |
|
647 |
resourceBundle = bundle; |
|
2 | 648 |
} catch (MissingResourceException ex) { |
649 |
// This is not a good place to throw an exception, |
|
650 |
// so we simply leave the resourceBundle null. |
|
651 |
resourceBundle = null; |
|
652 |
} |
|
653 |
} |
|
654 |
||
655 |
needToInferCaller = false; |
|
656 |
} |
|
657 |
||
658 |
// Private method to infer the caller's class and method names |
|
33875
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
659 |
// |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
660 |
// Note: |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
661 |
// For testing purposes - it is possible to customize the process |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
662 |
// by which LogRecord will infer the source class name and source method name |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
663 |
// when analyzing the call stack. |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
664 |
// <p> |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
665 |
// The system property {@code jdk.logger.packages} can define a comma separated |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
666 |
// list of strings corresponding to additional package name prefixes that |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
667 |
// should be ignored when trying to infer the source caller class name. |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
668 |
// Those stack frames whose {@linkplain StackTraceElement#getClassName() |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
669 |
// declaring class name} start with one such prefix will be ignored. |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
670 |
// <p> |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
671 |
// This is primarily useful when providing utility logging classes wrapping |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
672 |
// a logger instance, as it makes it possible to instruct LogRecord to skip |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
673 |
// those utility frames when inferring the caller source class name. |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
674 |
// <p> |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
675 |
// The {@code jdk.logger.packages} system property is consulted only once. |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
676 |
// <p> |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
677 |
// This property is not standard, implementation specific, and yet |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
678 |
// undocumented (and thus subject to changes without notice). |
c1c71107d45f
8140364: JEP 264 Platform Logger API and Service Implementation
dfuchs
parents:
32834
diff
changeset
|
679 |
// |
2 | 680 |
private void inferCaller() { |
681 |
needToInferCaller = false; |
|
34372 | 682 |
// Skip all frames until we have found the first logger frame. |
683 |
Optional<StackWalker.StackFrame> frame = new CallerFinder().get(); |
|
684 |
frame.ifPresent(f -> { |
|
685 |
setSourceClassName(f.getClassName()); |
|
686 |
setSourceMethodName(f.getMethodName()); |
|
687 |
}); |
|
2947
b0135c99348e
6511515: poor performance of LogRecord.inferCaller depending on java.lang.Throwable.getStackTraceElement
martin
parents:
2632
diff
changeset
|
688 |
|
2 | 689 |
// We haven't found a suitable frame, so just punt. This is |
690 |
// OK as we are only committed to making a "best effort" here. |
|
691 |
} |
|
7026
9f2ec5fad124
6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
5506
diff
changeset
|
692 |
|
34372 | 693 |
/* |
694 |
* CallerFinder is a stateful predicate. |
|
695 |
*/ |
|
696 |
static final class CallerFinder implements Predicate<StackWalker.StackFrame> { |
|
34723
734a1c90ce86
8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents:
34439
diff
changeset
|
697 |
private static final StackWalker WALKER; |
734a1c90ce86
8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents:
34439
diff
changeset
|
698 |
static { |
734a1c90ce86
8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents:
34439
diff
changeset
|
699 |
final PrivilegedAction<StackWalker> action = |
734a1c90ce86
8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents:
34439
diff
changeset
|
700 |
() -> StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE); |
734a1c90ce86
8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents:
34439
diff
changeset
|
701 |
WALKER = AccessController.doPrivileged(action); |
734a1c90ce86
8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents:
34439
diff
changeset
|
702 |
} |
34372 | 703 |
|
704 |
/** |
|
705 |
* Returns StackFrame of the caller's frame. |
|
706 |
* @return StackFrame of the caller's frame. |
|
707 |
*/ |
|
708 |
Optional<StackWalker.StackFrame> get() { |
|
709 |
return WALKER.walk((s) -> s.filter(this).findFirst()); |
|
710 |
} |
|
711 |
||
712 |
private boolean lookingForLogger = true; |
|
713 |
/** |
|
714 |
* Returns true if we have found the caller's frame, false if the frame |
|
715 |
* must be skipped. |
|
716 |
* |
|
717 |
* @param t The frame info. |
|
718 |
* @return true if we have found the caller's frame, false if the frame |
|
719 |
* must be skipped. |
|
720 |
*/ |
|
721 |
@Override |
|
722 |
public boolean test(StackWalker.StackFrame t) { |
|
723 |
final String cname = t.getClassName(); |
|
724 |
// We should skip all frames until we have found the logger, |
|
725 |
// because these frames could be frames introduced by e.g. custom |
|
726 |
// sub classes of Handler. |
|
727 |
if (lookingForLogger) { |
|
728 |
// the log record could be created for a platform logger |
|
729 |
lookingForLogger = !isLoggerImplFrame(cname); |
|
730 |
return false; |
|
731 |
} |
|
34723
734a1c90ce86
8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents:
34439
diff
changeset
|
732 |
// Continue walking until we've found the relevant calling frame. |
734a1c90ce86
8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents:
34439
diff
changeset
|
733 |
// Skips logging/logger infrastructure. |
734a1c90ce86
8145686: SimpleConsoleLogger and LogRecord should take advantage of StackWalker to skip classes implementing System.Logger
dfuchs
parents:
34439
diff
changeset
|
734 |
return !isFilteredFrame(t); |
34372 | 735 |
} |
736 |
||
737 |
private boolean isLoggerImplFrame(String cname) { |
|
738 |
return (cname.equals("java.util.logging.Logger") || |
|
739 |
cname.startsWith("sun.util.logging.PlatformLogger")); |
|
740 |
} |
|
7026
9f2ec5fad124
6985460: PlatformLogger throws ArrayStoreException when j.u.logging is initialized
mchung
parents:
5506
diff
changeset
|
741 |
} |
2 | 742 |
} |