qml日志类别的输出不会显示在控制台中

huangapple go评论119阅读模式
英文:

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:

  1. Import the necessary modules at the beginning of your QML code:
  1. import QtQuick 2.12
  2. import QtQuick.Window 2.12
  1. Create a QML Window:
  1. Window {
  2. visible: true
  3. width: 640
  4. height: 480
  5. title: qsTr("Hello World")
  1. Inside the Window, create an Item and define your LoggingCategory within it:
  1. Item {
  2. LoggingCategory {
  3. id: category
  4. name: "com.qt.category"
  5. defaultLogLevel: LoggingCategory.Warning
  6. }
  1. In the Component.onCompleted section of your Item, you can use console.log to output messages with different log levels:
  1. Component.onCompleted: {
  2. console.log("default message")
  3. console.log(category, "category message");
  4. }
  5. }
  6. }

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

  1. import QtQuick 2.12
  2. import QtQuick.Window 2.12
  3. Window {
  4. visible: true
  5. width: 640
  6. height: 480
  7. title: qsTr("Hello World")
  8. Item {
  9. LoggingCategory {
  10. id: category
  11. name: "com.qt.category"
  12. defaultLogLevel: LoggingCategory.Warning
  13. }
  14. Component.onCompleted: {
  15. console.log("default message")
  16. console.log(category, "category message");
  17. }
  18. }
  19. }

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:

  1. import QtQuick 2.12
  2. import QtQuick.Window 2.12
  3. Window {
  4. visible: true
  5. width: 640
  6. height: 480
  7. title: qsTr("Hello World")
  8. Item {
  9. LoggingCategory {
  10. id: qml_custom_debug
  11. name: "qml.custom.debug"
  12. defaultLogLevel: LoggingCategory.Debug
  13. }
  14. LoggingCategory {
  15. id: qml_custom_info
  16. name: "qml.custom.info"
  17. defaultLogLevel: LoggingCategory.Info
  18. }
  19. LoggingCategory {
  20. id: qml_custom_warn
  21. name: "qml.custom.warn"
  22. defaultLogLevel: LoggingCategory.Warning
  23. }
  24. LoggingCategory {
  25. id: qml_custom_critical
  26. name: "qml.custom.critical"
  27. defaultLogLevel: LoggingCategory.Critical
  28. }
  29. Component.onCompleted: {
  30. console.log("Component.onCompleted")
  31. console.log(qml_custom_debug, "category debug - log: Component.onCompleted")
  32. console.info(qml_custom_debug, "category debug - info: Component.onCompleted")
  33. console.warn(qml_custom_debug, "category debug - warn: Component.onCompleted")
  34. console.error(qml_custom_debug, "category debug - error: Component.onCompleted")
  35. console.debug(qml_custom_debug, "category debug - debug: Component.onCompleted")
  36. console.log(qml_custom_info, "category info - log: Component.onCompleted")
  37. console.info(qml_custom_info, "category info - info: Component.onCompleted")
  38. console.warn(qml_custom_info, "category info - warn: Component.onCompleted")
  39. console.error(qml_custom_info, "category info - error: Component.onCompleted")
  40. console.debug(qml_custom_info, "category info - debug: Component.onCompleted")
  41. console.log(qml_custom_warn, "category warn - log: Component.onCompleted")
  42. console.info(qml_custom_warn, "category warn - info: Component.onCompleted")
  43. console.warn(qml_custom_warn, "category warn - warn: Component.onCompleted")
  44. console.error(qml_custom_warn, "category warn - error: Component.onCompleted")
  45. console.debug(qml_custom_warn, "category warn - debug: Component.onCompleted")
  46. console.log(qml_custom_critical, "category critical - log: Component.onCompleted")
  47. console.info(qml_custom_critical, "category critical - info: Component.onCompleted")
  48. console.warn(qml_custom_critical, "category critical - warn: Component.onCompleted")
  49. console.error(qml_custom_critical, "category critical - error: Component.onCompleted")
  50. console.debug(qml_custom_critical, "category critical - debug: Component.onCompleted")
  51. }
  52. }
  53. }

Output

  1. qml: Component.onCompleted
  2. qml.custom.debug: category debug - log: Component.onCompleted
  3. qml.custom.debug: category debug - info: Component.onCompleted
  4. qml.custom.debug: category debug - warn: Component.onCompleted
  5. qml.custom.debug: category debug - error: Component.onCompleted
  6. qml.custom.debug: category debug - debug: Component.onCompleted
  7. qml.custom.info: category info - info: Component.onCompleted
  8. qml.custom.info: category info - warn: Component.onCompleted
  9. qml.custom.info: category info - error: Component.onCompleted
  10. qml.custom.warn: category warn - warn: Component.onCompleted
  11. qml.custom.warn: category warn - error: Component.onCompleted
  12. 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

  1. QLoggingCategory::setFilterRules(
  2. QStringLiteral("*.info=false\n"
  3. "*.debug=false\n");

答案1

得分: 0

defaultLogLevel 是你的消息过滤器。如果你使用 defaultLogLevel 的 Warning 调用 console.log(),消息将被过滤掉。调用 console.warn() 将显示消息。

你可以尝试一下日志级别和默认日志级别,然后看看发生了什么:

  1. Item {
  2. LoggingCategory {
  3. id: category
  4. name: "com.qt.category"
  5. // defaultLogLevel: LoggingCategory.Debug
  6. // defaultLogLevel: LoggingCategory.Warning
  7. defaultLogLevel: LoggingCategory.Critical
  8. }
  9. Component.onCompleted: {
  10. console.log("debug message");
  11. console.warn(category, "warn message");
  12. console.error(category, "error message");
  13. }
  14. }
英文:

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:

  1. Item {
  2. LoggingCategory {
  3. id: category
  4. name: "com.qt.category"
  5. // defaultLogLevel: LoggingCategory.Debug
  6. // defaultLogLevel: LoggingCategory.Warning
  7. defaultLogLevel: LoggingCategory.Critical
  8. }
  9. Component.onCompleted: {
  10. console.log("debug message");
  11. console.warn(category, "warn message");
  12. console.error(category, "error message");
  13. }
  14. }

huangapple
  • 本文由 发表于 2023年7月20日 14:53:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727356.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定