Java 9 Platform Logging API and Service

https://grokonez.com/java/java-9/java-9-platform-logging-and-service

Java 9 Platform Logging API and Service

Java 9 defines a minimal logging API which platform classes can use to log messages, together with a service interface for consumers of those messages. In this tutorial, we're gonna take a look at new Java 9 Platform Logging API and an example that implements LoggerFinder service which can route platform log messages to the logging framework for application logger instead of system logger.

I. Platform Logging API and Service

An implementation of LoggerFinder is loaded with help of java.util.ServiceLoader API using system class loader. Basing on this implementation, an application/framework can plug in its own external logging backend, without configuring java.util.logging or that backend.

We can pass the class name or module (related to specific Logger) to the LoggerFinder so that the LoggerFinder can know which logger to return.


public class MyLoggerFinder extends LoggerFinder {

    @Override
    public Logger getLogger(String name, Module module) {
        // return Logger depending on name/module...
    }
}

If no concrete implementation is found, JDK default LoggerFinder implementation will be used. java.util.logging (in java.logging module) now becomes backend. So log messages will be routed to java.util.logging.Logger.

We obtain loggers that are created from the LoggerFinder using factory methods of the System class:


package java.lang;
...
public class System {
    System.Logger getLogger(String name) { ... }
    System.Logger getLogger(String name, ResourceBundle bundle) { ... }
}

II. Example

1. Overview

In logger package:

  • We will create 2 concrete Loggers that implement Logger interface: MyLogger and MyNewLogger.
  • MyLoggerFinder is an implementation of LoggerFinder that can return desired Logger by name. We build logger package into demo.logging module. This module provides an implementation of LoggerFinder service.

In app packagee, we create a Test Class that uses System factory method getLogger(name) to get Loggers and use them.
We build app package into demo.app module.

Folder tree should be:

More at:

https://grokonez.com/java/java-9/java-9-platform-logging-and-service

Java 9 Platform Logging API and Service

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

545 bài viết.
97 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 7 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 6 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/java/java9/java9flowapireactivestreams Java 9 Flow API – Reactive Streams Java 9 introduces Reactive Streams under java.util...
loveprogramming viết 2 tháng trước
0 0
White
19 2
PayPal, reddit, Youtube hay bất cứ trang nào sống nhờ nội dung do người dùng tạo ra (usergenerated contents), dù có nổi tiếng đến đâu thì đều trải ...
Tuan-Anh Hoang-Vu viết 6 năm trước
19 2
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
545 bài viết.
97 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!