本页内容

LoggingCategory QML Type

在 QML 中定义一个日志类别。更多内容...

Import Statement: import QtQml

属性

详细说明

可以将日志类别作为第一个参数传递给 console.log() 及其相关函数。如果将该参数提供给日志器,则会使用 LoggingCategory 的名称作为日志类别。否则,将使用默认的日志类别。

import QtQuick

Item {
    LoggingCategory {
        id: category
        name: "com.qt.category"
        defaultLogLevel: LoggingCategory.Warning
    }

    Component.onCompleted: {
        console.log(category, "log message");
        console.warn(category, "warning message");
    }
}

默认情况下,这仅会输出“com.qt.category: warning message ”。由于启用了defaultLogLevel ,log message 将被抑制。不过,您可以像配置QLoggingCategory 那样,为QML日志类别配置日志级别。

注意:由于 对象的创建开销较大,建议将所需的 LoggingCategory 定义放入单例中,并在需要时导入该单例。

另请参阅 QLoggingCategory 。

属性文档

defaultLogLevel : enumeration

保存日志类别的默认日志级别。默认情况下,该类别创建时使用LoggingCategory.Debug日志级别。

可用的枚举值如下:

它们反映了QtMsgType 枚举的值。

注意:此 属性需在声明LoggingCategory 时设置,之后无法更改。

另请参阅 QtMsgType 。

name : string

存储日志类别的名称。

注意:该 属性需在声明LoggingCategory 时设置,之后无法更改。

另请参阅 QLoggingCategory::categoryName()。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.