121 */
122 private int threadID;
123
124 /**
125 * The Throwable (if any) associated with log message
126 */
127 private Throwable thrown;
128
129 /**
130 * Name of the source Logger.
131 */
132 private String loggerName;
133
134 /**
135 * Resource bundle name to localized log message.
136 */
137 private String resourceBundleName;
138
139 /**
140 * Event time.
141 * @since 1.9
142 */
143 private Instant instant;
144
145 /**
146 * @serialField level Level Logging message level
147 * @serialField sequenceNumber long Sequence number
148 * @serialField sourceClassName String Class that issued logging call
149 * @serialField sourceMethodName String Method that issued logging call
150 * @serialField message String Non-localized raw message text
151 * @serialField threadID int Thread ID for thread that issued logging call
152 * @serialField millis long Truncated event time in milliseconds since 1970
153 * - calculated as getInstant().toEpochMilli().
154 * The event time instant can be reconstructed using
155 * <code>Instant.ofEpochSecond(millis/1000, (millis % 1000) * 1000_000 + nanoAdjustment)</code>
156 * @serialField nanoAdjustment int Nanoseconds adjustment to the millisecond of
157 * event time - calculated as getInstant().getNano() % 1000_000
158 * The event time instant can be reconstructed using
159 * <code>Instant.ofEpochSecond(millis/1000, (millis % 1000) * 1000_000 + nanoAdjustment)</code>
160 * <p>
161 * Since: 1.9
162 * @serialField thrown Throwable The Throwable (if any) associated with log
163 * message
164 * @serialField loggerName String Name of the source Logger
165 * @serialField resourceBundleName String Resource bundle name to localized
166 * log message
167 */
168 private static final ObjectStreamField[] serialPersistentFields =
169 new ObjectStreamField[] {
170 new ObjectStreamField("level", Level.class),
171 new ObjectStreamField("sequenceNumber", long.class),
172 new ObjectStreamField("sourceClassName", String.class),
173 new ObjectStreamField("sourceMethodName", String.class),
174 new ObjectStreamField("message", String.class),
175 new ObjectStreamField("threadID", int.class),
176 new ObjectStreamField("millis", long.class),
177 new ObjectStreamField("nanoAdjustment", int.class),
178 new ObjectStreamField("thrown", Throwable.class),
179 new ObjectStreamField("loggerName", String.class),
180 new ObjectStreamField("resourceBundleName", String.class),
181 };
190 private int defaultThreadID() {
191 long tid = Thread.currentThread().getId();
192 if (tid < MIN_SEQUENTIAL_THREAD_ID) {
193 return (int) tid;
194 } else {
195 Integer id = threadIds.get();
196 if (id == null) {
197 id = nextThreadId.getAndIncrement();
198 threadIds.set(id);
199 }
200 return id;
201 }
202 }
203
204 /**
205 * Construct a LogRecord with the given level and message values.
206 * <p>
207 * The sequence property will be initialized with a new unique value.
208 * These sequence values are allocated in increasing order within a VM.
209 * <p>
210 * Since JDK 1.9, the event time is represented by an {@link Instant}.
211 * The instant property will be initialized to the {@linkplain
212 * Instant#now() current instant}, using the best available
213 * {@linkplain Clock#systemUTC() clock} on the system.
214 * <p>
215 * The thread ID property will be initialized with a unique ID for
216 * the current thread.
217 * <p>
218 * All other properties will be initialized to "null".
219 *
220 * @param level a logging level value
221 * @param msg the raw non-localized logging message (may be null)
222 * @see java.time.Clock#systemUTC()
223 */
224 public LogRecord(Level level, String msg) {
225 this.level = Objects.requireNonNull(level);
226 message = msg;
227 // Assign a thread ID and a unique sequence number.
228 sequenceNumber = globalSequenceNumber.getAndIncrement();
229 threadID = defaultThreadID();
230 instant = Instant.now();
488 * {@link #setInstant(java.time.Instant)
489 * setInstant(Instant.ofEpochMilli(millis))}.
490 *
491 * @deprecated LogRecord maintains timestamps with nanosecond resolution,
492 * using {@link Instant} values. For this reason,
493 * {@link #setInstant(java.time.Instant) setInstant()}
494 * should be used in preference to {@code setMillis()}.
495 *
496 * @see #setInstant(java.time.Instant)
497 */
498 @Deprecated
499 public void setMillis(long millis) {
500 this.instant = Instant.ofEpochMilli(millis);
501 }
502
503 /**
504 * Gets the instant that the event occurred.
505 *
506 * @return the instant that the event occurred.
507 *
508 * @since 1.9
509 */
510 public Instant getInstant() {
511 return instant;
512 }
513
514 /**
515 * Sets the instant that the event occurred.
516 * <p>
517 * If the given {@code instant} represents a point on the time-line too
518 * far in the future or past to fit in a {@code long} milliseconds and
519 * nanoseconds adjustment, then an {@code ArithmeticException} will be
520 * thrown.
521 *
522 * @param instant the instant that the event occurred.
523 *
524 * @throws NullPointerException if {@code instant} is null.
525 * @throws ArithmeticException if numeric overflow would occur while
526 * calling {@link Instant#toEpochMilli() instant.toEpochMilli()}.
527 *
528 * @since 1.9
529 */
530 public void setInstant(Instant instant) {
531 instant.toEpochMilli();
532 this.instant = instant;
533 }
534
535 /**
536 * Get any throwable associated with the log record.
537 * <p>
538 * If the event involved an exception, this will be the
539 * exception object. Otherwise null.
540 *
541 * @return a throwable
542 */
543 public Throwable getThrown() {
544 return thrown;
545 }
546
547 /**
548 * Set a throwable associated with the log event.
|
121 */
122 private int threadID;
123
124 /**
125 * The Throwable (if any) associated with log message
126 */
127 private Throwable thrown;
128
129 /**
130 * Name of the source Logger.
131 */
132 private String loggerName;
133
134 /**
135 * Resource bundle name to localized log message.
136 */
137 private String resourceBundleName;
138
139 /**
140 * Event time.
141 * @since 9
142 */
143 private Instant instant;
144
145 /**
146 * @serialField level Level Logging message level
147 * @serialField sequenceNumber long Sequence number
148 * @serialField sourceClassName String Class that issued logging call
149 * @serialField sourceMethodName String Method that issued logging call
150 * @serialField message String Non-localized raw message text
151 * @serialField threadID int Thread ID for thread that issued logging call
152 * @serialField millis long Truncated event time in milliseconds since 1970
153 * - calculated as getInstant().toEpochMilli().
154 * The event time instant can be reconstructed using
155 * <code>Instant.ofEpochSecond(millis/1000, (millis % 1000) * 1000_000 + nanoAdjustment)</code>
156 * @serialField nanoAdjustment int Nanoseconds adjustment to the millisecond of
157 * event time - calculated as getInstant().getNano() % 1000_000
158 * The event time instant can be reconstructed using
159 * <code>Instant.ofEpochSecond(millis/1000, (millis % 1000) * 1000_000 + nanoAdjustment)</code>
160 * <p>
161 * Since: 9
162 * @serialField thrown Throwable The Throwable (if any) associated with log
163 * message
164 * @serialField loggerName String Name of the source Logger
165 * @serialField resourceBundleName String Resource bundle name to localized
166 * log message
167 */
168 private static final ObjectStreamField[] serialPersistentFields =
169 new ObjectStreamField[] {
170 new ObjectStreamField("level", Level.class),
171 new ObjectStreamField("sequenceNumber", long.class),
172 new ObjectStreamField("sourceClassName", String.class),
173 new ObjectStreamField("sourceMethodName", String.class),
174 new ObjectStreamField("message", String.class),
175 new ObjectStreamField("threadID", int.class),
176 new ObjectStreamField("millis", long.class),
177 new ObjectStreamField("nanoAdjustment", int.class),
178 new ObjectStreamField("thrown", Throwable.class),
179 new ObjectStreamField("loggerName", String.class),
180 new ObjectStreamField("resourceBundleName", String.class),
181 };
190 private int defaultThreadID() {
191 long tid = Thread.currentThread().getId();
192 if (tid < MIN_SEQUENTIAL_THREAD_ID) {
193 return (int) tid;
194 } else {
195 Integer id = threadIds.get();
196 if (id == null) {
197 id = nextThreadId.getAndIncrement();
198 threadIds.set(id);
199 }
200 return id;
201 }
202 }
203
204 /**
205 * Construct a LogRecord with the given level and message values.
206 * <p>
207 * The sequence property will be initialized with a new unique value.
208 * These sequence values are allocated in increasing order within a VM.
209 * <p>
210 * Since JDK 9, the event time is represented by an {@link Instant}.
211 * The instant property will be initialized to the {@linkplain
212 * Instant#now() current instant}, using the best available
213 * {@linkplain Clock#systemUTC() clock} on the system.
214 * <p>
215 * The thread ID property will be initialized with a unique ID for
216 * the current thread.
217 * <p>
218 * All other properties will be initialized to "null".
219 *
220 * @param level a logging level value
221 * @param msg the raw non-localized logging message (may be null)
222 * @see java.time.Clock#systemUTC()
223 */
224 public LogRecord(Level level, String msg) {
225 this.level = Objects.requireNonNull(level);
226 message = msg;
227 // Assign a thread ID and a unique sequence number.
228 sequenceNumber = globalSequenceNumber.getAndIncrement();
229 threadID = defaultThreadID();
230 instant = Instant.now();
488 * {@link #setInstant(java.time.Instant)
489 * setInstant(Instant.ofEpochMilli(millis))}.
490 *
491 * @deprecated LogRecord maintains timestamps with nanosecond resolution,
492 * using {@link Instant} values. For this reason,
493 * {@link #setInstant(java.time.Instant) setInstant()}
494 * should be used in preference to {@code setMillis()}.
495 *
496 * @see #setInstant(java.time.Instant)
497 */
498 @Deprecated
499 public void setMillis(long millis) {
500 this.instant = Instant.ofEpochMilli(millis);
501 }
502
503 /**
504 * Gets the instant that the event occurred.
505 *
506 * @return the instant that the event occurred.
507 *
508 * @since 9
509 */
510 public Instant getInstant() {
511 return instant;
512 }
513
514 /**
515 * Sets the instant that the event occurred.
516 * <p>
517 * If the given {@code instant} represents a point on the time-line too
518 * far in the future or past to fit in a {@code long} milliseconds and
519 * nanoseconds adjustment, then an {@code ArithmeticException} will be
520 * thrown.
521 *
522 * @param instant the instant that the event occurred.
523 *
524 * @throws NullPointerException if {@code instant} is null.
525 * @throws ArithmeticException if numeric overflow would occur while
526 * calling {@link Instant#toEpochMilli() instant.toEpochMilli()}.
527 *
528 * @since 9
529 */
530 public void setInstant(Instant instant) {
531 instant.toEpochMilli();
532 this.instant = instant;
533 }
534
535 /**
536 * Get any throwable associated with the log record.
537 * <p>
538 * If the event involved an exception, this will be the
539 * exception object. Otherwise null.
540 *
541 * @return a throwable
542 */
543 public Throwable getThrown() {
544 return thrown;
545 }
546
547 /**
548 * Set a throwable associated with the log event.
|