英文:
qml logging category output not shown in console
问题
To make the QML logging category work and show output in the console, you can follow these steps:
- Import the necessary modules at the beginning of your QML code:
import QtQuick 2.12
import QtQuick.Window 2.12
- Create a QML Window:
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
- Inside the Window, create an Item and define your LoggingCategory within it:
Item {
LoggingCategory {
id: category
name: "com.qt.category"
defaultLogLevel: LoggingCategory.Warning
}
- In the
Component.onCompleted
section of your Item, you can useconsole.log
to output messages with different log levels:
Component.onCompleted: {
console.log("default message")
console.log(category, "category message");
}
}
}
That's the basic setup for using QML logging categories. You can specify the log levels and messages as needed, as you demonstrated in your updates. It's also important to note that QML logging can be influenced by settings made in C++, as mentioned in your second update, using QLoggingCategory::setFilterRules
to control which log levels are displayed for different categories.
I hope this helps with your QML logging configuration!
英文:
What is needed to make the qml logging category work, e.g. show output in the console?
Currently only "default message"
is shown. I expected also "category message"
to appear in the console. A different log level, for example LoggingCategory.Info
, also did not help.
https://doc.qt.io/qt-5/qml-qtqml-loggingcategory.html
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Item {
LoggingCategory {
id: category
name: "com.qt.category"
defaultLogLevel: LoggingCategory.Warning
}
Component.onCompleted: {
console.log("default message")
console.log(category, "category message");
}
}
}
Update:
After the answer I played around to see combinations that work... not really intuitive in my opinion, unlike most of Qt's great documentation
Summarizing working combinations:
- LoggingCategory.Debug -> console.(log/info/warn/error/debug)
- LoggingCategory.Info-> console.(info/warn/error)
- LoggingCategory.Warning-> console.(warn/error)
- LoggingCategory.Critical -> console.(error)
Playground:
import QtQuick 2.12
import QtQuick.Window 2.12
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Item {
LoggingCategory {
id: qml_custom_debug
name: "qml.custom.debug"
defaultLogLevel: LoggingCategory.Debug
}
LoggingCategory {
id: qml_custom_info
name: "qml.custom.info"
defaultLogLevel: LoggingCategory.Info
}
LoggingCategory {
id: qml_custom_warn
name: "qml.custom.warn"
defaultLogLevel: LoggingCategory.Warning
}
LoggingCategory {
id: qml_custom_critical
name: "qml.custom.critical"
defaultLogLevel: LoggingCategory.Critical
}
Component.onCompleted: {
console.log("Component.onCompleted")
console.log(qml_custom_debug, "category debug - log: Component.onCompleted")
console.info(qml_custom_debug, "category debug - info: Component.onCompleted")
console.warn(qml_custom_debug, "category debug - warn: Component.onCompleted")
console.error(qml_custom_debug, "category debug - error: Component.onCompleted")
console.debug(qml_custom_debug, "category debug - debug: Component.onCompleted")
console.log(qml_custom_info, "category info - log: Component.onCompleted")
console.info(qml_custom_info, "category info - info: Component.onCompleted")
console.warn(qml_custom_info, "category info - warn: Component.onCompleted")
console.error(qml_custom_info, "category info - error: Component.onCompleted")
console.debug(qml_custom_info, "category info - debug: Component.onCompleted")
console.log(qml_custom_warn, "category warn - log: Component.onCompleted")
console.info(qml_custom_warn, "category warn - info: Component.onCompleted")
console.warn(qml_custom_warn, "category warn - warn: Component.onCompleted")
console.error(qml_custom_warn, "category warn - error: Component.onCompleted")
console.debug(qml_custom_warn, "category warn - debug: Component.onCompleted")
console.log(qml_custom_critical, "category critical - log: Component.onCompleted")
console.info(qml_custom_critical, "category critical - info: Component.onCompleted")
console.warn(qml_custom_critical, "category critical - warn: Component.onCompleted")
console.error(qml_custom_critical, "category critical - error: Component.onCompleted")
console.debug(qml_custom_critical, "category critical - debug: Component.onCompleted")
}
}
}
Output
qml: Component.onCompleted
qml.custom.debug: category debug - log: Component.onCompleted
qml.custom.debug: category debug - info: Component.onCompleted
qml.custom.debug: category debug - warn: Component.onCompleted
qml.custom.debug: category debug - error: Component.onCompleted
qml.custom.debug: category debug - debug: Component.onCompleted
qml.custom.info: category info - info: Component.onCompleted
qml.custom.info: category info - warn: Component.onCompleted
qml.custom.info: category info - error: Component.onCompleted
qml.custom.warn: category warn - warn: Component.onCompleted
qml.custom.warn: category warn - error: Component.onCompleted
qml.custom.critical: category critical - error: Component.onCompleted
Update 2:
Also learned that them qml logging also is influenced by settings made in c++, for example
QLoggingCategory::setFilterRules(
QStringLiteral("*.info=false\n"
"*.debug=false\n");
答案1
得分: 0
defaultLogLevel
是你的消息过滤器。如果你使用 defaultLogLevel
的 Warning 调用 console.log()
,消息将被过滤掉。调用 console.warn()
将显示消息。
你可以尝试一下日志级别和默认日志级别,然后看看发生了什么:
Item {
LoggingCategory {
id: category
name: "com.qt.category"
// defaultLogLevel: LoggingCategory.Debug
// defaultLogLevel: LoggingCategory.Warning
defaultLogLevel: LoggingCategory.Critical
}
Component.onCompleted: {
console.log("debug message");
console.warn(category, "warn message");
console.error(category, "error message");
}
}
英文:
The defaultLogLevel
is the filter for your messages. If you call console.log()
with defaultLogLevel
of Warning, the message is filtered out. A call to console.warn()
will be shown.
You can play a bit with log levels and default log levels than you see what is happening:
Item {
LoggingCategory {
id: category
name: "com.qt.category"
// defaultLogLevel: LoggingCategory.Debug
// defaultLogLevel: LoggingCategory.Warning
defaultLogLevel: LoggingCategory.Critical
}
Component.onCompleted: {
console.log("debug message");
console.warn(category, "warn message");
console.error(category, "error message");
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论