Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loggerの利用方法 #14

Open
TodorokiKohei opened this issue Jul 6, 2023 · 6 comments
Open

Loggerの利用方法 #14

TodorokiKohei opened this issue Jul 6, 2023 · 6 comments

Comments

@TodorokiKohei
Copy link
Owner

標準で提供されているLoggerを利用する手順を書く

@TodorokiKohei
Copy link
Owner Author

基本は公式ページに説明されてある。
https://wiki.eclipse.org/Paho/Log_and_Debug_in_the_Java_client

@TodorokiKohei
Copy link
Owner Author

TodorokiKohei commented Jul 6, 2023

Javaの標準パッケージであるjava.util.loggingを使用している。仕様はJSR47JavaDocを見るように書いてある。

JSRは"Java Specification Request"でJavaの仕様のことらしい。RFCのようなもの?

@TodorokiKohei
Copy link
Owner Author

TodorokiKohei commented Jul 6, 2023

Loggerの設定方法として、-Dオプションで設定ファイルのパスを渡す方法がある。
-Djava.util.logging.config.file=<パス>/<設定ファイル>をJava実行時の引数で指定することで設定が反映される。
(-Dkey=valオプションはJavaのシステムプロパティになるようです(参考System.getProperty(ket)で取得可能)
設定ファイルは以下のような記述になる。

# Properties file which configures the operation of the JDK logging facility.
#
# The configuration in this file is the suggesgted configuration
# for collecting trace for helping debug problems related to the
# Paho MQTT client.  It configures trace to be continuosly collected
# in memory with minimal impact on performance.
#
# When the push trigger (by default a Severe level message) or a
# specific request is made to "push" the in memory trace then it
# is "pushed" to the configured target handler. By default
# this is the standard java.util.logging.FileHandler. The Paho Debug
# class can be used to push the memory trace to its target
#
# To enable trace either:
# - use this properties file as is and set the logging facility up
#   to use it by configuring the util logging system property e.g.
#
# >java -Djava.util.logging.config.file=<location>\jsr47min.properties
#
# - This contents of this file can also be merged with another
#   java.util.logging config file to ensure provide wider logging
#   and trace including Paho trace

# Global logging properties.
# ------------------------------------------
# The set of handlers to be loaded upon startup.
# Comma-separated list of class names.
# - Root handlers are not enabled by default - just handlers on the Paho packages.
#handlers=java.util.logging.MemoryHandler,java.util.logging.FileHandler, java.util.logging.ConsoleHandler

# Default global logging level.
# Loggers and Handlers may override this level
#.level=INFO

# Loggers
# ------------------------------------------
# A memoryhandler is attached to the paho packages
# and the level specified to collected all trace related
# to paho packages.  This will override any root/global
# level handlers if set.
org.eclipse.paho.mqttv5.client.handlers=java.util.logging.MemoryHandler
org.eclipse.paho.mqttv5.client.level=ALL
# It is possible to set more granular trace on a per class basis e.g.
#org.eclipse.paho.mqttv5.client.internal.ClientComms.level=ALL

# Handlers
# -----------------------------------------
# Note: the target handler that is associated with the MemoryHandler is not a root handler
# and hence not returned when getting the handlers from root. It appears accessing
# target handler programatically is not possible as target is a private variable in
# class MemoryHandler
java.util.logging.MemoryHandler.level=ALL
java.util.logging.MemoryHandler.size=10000
java.util.logging.MemoryHandler.push=ALL
#java.util.logging.MemoryHandler.target=java.util.logging.FileHandler
java.util.logging.MemoryHandler.target=java.util.logging.ConsoleHandler


# --- FileHandler ---
# Override of global logging level
java.util.logging.FileHandler.level=INFO

# Naming style for the output file:
# (The output file is placed in the directory
# defined by the "user.home" System property.)
# See java.util.logging for more options
java.util.logging.FileHandler.pattern=%h/ibm/paho/trace/paho%u.log

# Limiting size of output file in bytes:
java.util.logging.FileHandler.limit=200000

# Number of output files to cycle through, by appending an
# integer to the base file name:
java.util.logging.FileHandler.count=3

# Style of output (Simple or XML):
java.util.logging.FileHandler.formatter=org.eclipse.paho.mqttv5.client.logging.SimpleLogFormatter

# --- ConsoleHandler ---
# Override of global logging level
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.formatter=org.eclipse.paho.mqttv5.client.logging.SimpleLogFormatter

@TodorokiKohei
Copy link
Owner Author

TodorokiKohei commented Jul 6, 2023

実行時の表示はこんな感じ。左から順に
Level Data & Time Class Method Thread clientID Message
が表示されてる。
スレッド別のログが表示されているため、cutgrepを利用することで特定の処理がどのスレッドのどのクラスで実行されているかがトレースできる。
image

@TodorokiKohei
Copy link
Owner Author

java.util.loggingはパッケージ毎にロガーの設定を変更することができる.

# グローバルな設定
handlers=java.util.logging.MemoryHandler
.level=FINE

# パッケージ毎の設定
org.eclipse.paho.mqttv5.client.handlers=java.util.logging.MemoryHandler
org.eclipse.paho.mqttv5.client.level=INFO

この設定でorg.eclipse.paho.mqttv5.client.handlersのパッケージのみレベルがINFOになり,他のパッケージはFINEになる.

@TodorokiKohei
Copy link
Owner Author

TodorokiKohei commented Oct 19, 2023

このパッケージではロギングメッセージをorg.eclipse.paho.mqttv5.client.internal.nls.logcatに格納している.
これはメッセージのローカライズに利用する手法らしい.

ResouceBundleでロギングメッセージを格納したプロパティファイルを読み込むことでメッセージを柔軟に変更することができる.
例えば,クラスパス上に以下のプロパティファイルを作成する.(プロパティファイルの仕様:BaseName[_Language][_Country][_Variant].properties)

MyMessages_en_US.properties

greeting=Hello

MyMessages_ja_JP.properties

greeting=こんにちは

以下のようなJavaファイルを作成することでログメッセージを切り替えることができる.

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.logging.Logger;

public class LocaleChangeExample {
    public static void main(String[] args) {
        // 英語のResourceBundleを使用するLogger
        ResourceBundle bundleEN = ResourceBundle.getBundle("MyMessages", Locale.US);
        Logger loggerEN = Logger.getLogger("TestLogger", bundleEN.getBaseBundleName());

        // ログ出力(英語)
        loggerEN.info(loggerEN.getResourceBundle().getString("greeting"));  // Output: Hello

        // 日本語のResourceBundleを使用するLogger
        ResourceBundle bundleJA = ResourceBundle.getBundle("MyMessages", Locale.JAPAN);
        Logger loggerJA = Logger.getLogger("TestLogger", bundleJA.getBaseBundleName());

        // ログ出力(日本語)
        loggerJA.info(loggerJA.getResourceBundle().getString("greeting"));  // Output: こんにちは
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant