gradle学习(19)-log系统

Gradle日志系统详解
本文详细介绍了Gradle的日志系统,包括日志信息的分类、如何选择不同的日志等级、自定义日志信息的方法、如何调整外部工具的输出级别等。此外,还探讨了如何为特定任务设置日志等级及自定义Gradle日志记录器。

1.log信息的分类


除了常用的 debug,info,warning,error ,gradle自己特有的quiet和lifecycle。


2.选择log等级


-q/--quiet

-i//--info

-d/--debug


引申出

-s/--stacktrace

-S/--full-stacktrace


3.编写自己的log信息


println 后跟信息,gradle会将其重定向到日志系统中,默认为quiet等级。

当然你可以使用logger属性来编写不同等级的,继承与slf4j logger接口,然后加上一些gradle自己的定义。


logger.quiet('An info log message which is always logged.')
logger.error('An error log message.')
logger.warn('A warning log message.')
logger.lifecycle('A lifecycle info log message.')
logger.info('An info log message.')
logger.debug('A debug log message.')
logger.trace('A trace log message.')

qianhuis-Mac-mini:0112 qianhui$ gradle
An info log message which is always logged.
An error log message.
A warning log message.
A lifecycle info log message.
:help

Welcome to Gradle 2.2.1.

To run a build, run gradle <task> ...

To see a list of available tasks, run gradle tasks

To see a list of command-line options, run gradle --help

BUILD SUCCESSFUL

Total time: 2.265 secs

4.SLF4J日志


import org.slf4j.Logger
import org.slf4j.LoggerFactory
Logger slf4jLogger = LoggerFactory.getLogger('some-logger')
slf4jLogger.info('An info log message logged using SLF4j')

5.外部工具和库的logging


因为gradle是基于ant喝ivy的,所以前辈的logging输出应该被考虑,gradle会把它们的log重定向到gradle的log系统中,对于gradle有的等级,可以很好

的重定向,而对于gradle没有的,比如ant和ivy的trace等级,gradle会将其映射为debug等级,说明gradle默认情况智慧输出ant/ivy 的error和warning信息。

对于一些使用标准输出的工具,默认情况下,gradle重定向为quiet等级,标准的错误会被定义为error等级,但是这都是可以配置的。project对象提供一个LoggingManager,

该对象允许你去改变默认的重定向log等级。


logging.captureStandardOutput LogLevel.INFO

println 'A message which is logged at info level'

将log等级定位成info等级,执行命令后,如果加-q后会无法显示:


qianhuis-Mac-mini:0112 qianhui$ gradle -q

Welcome to Gradle 2.2.1.

To run a build, run gradle <task> ...

To see a list of available tasks, run gradle tasks

To see a list of command-line options, run gradle --help

6.为任务定义个log等级


println 'A message which is logged at info level'
task logInfo {
    logging.captureStandardOutput LogLevel.INFO
    doFirst {
        println 'A task message which is logged at INFO level'
}}
println 'end'

执行命令后会发现,任务中的log被隐藏掉了,因为任务中定义了log的等级为info,小于quiet等级,所以没有输出。说明任务内定义log等级只在任务内有效。


qianhuis-Mac-mini:0112 qianhui$ gradle -q logInfo
A message which is logged at info level
end


7.改变gradle日志


使用Gradle.useLogger()方法,具体做法是通过API中的接口重新定义log器。


useLogger(new CustomEventLogger())
class CustomEventLogger extends BuildAdapter implements TaskExecutionListener {
    public void beforeExecute(Task task) {
        println "[$task.name]"
}
    public void afterExecute(Task task, TaskState state) {
        println()
}
    public void buildFinished(BuildResult result) {
        println 'build completed'
        if (result.failure != null) {
        
 } }

但是上面的程序暂时还无法运行成功,暂时不太清楚为什么,以后再找答案。


要想自定义log器,可以研究下面几个接口:


BuildListener

ProjectEvaluationListener

TaskExecutionGraphListener

TaskExecutionListener

TaskActionListener


PS C:\Users\forgi\AndroidStudioProjects\ncnn-android-yolov8\ncnn-android-yolov8> ./gradlew cleanBuildCache Welcome to Gradle 7.6! Here are the highlights of this release: - Added support for Java 19. - Introduced `--rerun` flag for individual task rerun. - Improved dependency block for test suites to be strongly typed. - Added a pluggable system for Java toolchains provisioning. For more details see https://docs.gradle.org/7.6/release-notes.html Starting a Gradle Daemon, 1 incompatible Daemon could not be reused, use --status for details > Configure project :app C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. WARNING:C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. C/C++: Ignoring invalid ABI 'riscv64' found in ABI metadata file 'C:\Users\forgi\AppData\Local\Android\Sdk\ndk\29.0.13846066\meta\abis.json'. FAILURE: Build failed with an exception. * What went wrong: Task 'cleanBuildCache' not found in root project 'ncnn-android-yolov8' and its subprojects. * Try: > Run gradlew tasks to get a list of available tasks. > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0. You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. See https://docs.gradle.org/7.6/userguide/command_line_interface.html#sec:command_line_warnings BUILD FAILED in 7s PS C:\Users\forgi\AndroidStudioProjects\ncnn-android-yolov8\ncnn-android-yolov8> ./gradlew --recompile-scripts Unknown command-line option '--recompile-scripts'. USAGE: gradlew [option...] [task...] -?, -h, --help Shows this help message. -a, --no-rebuild Do not rebuild project dependencies. -b, --build-file Specify the build file. [deprecated] --build-cache Enables the Gradle build cache. Gradle will try to reuse outputs from previous builds. -c, --settings-file Specify the settings file. [deprecated] --configuration-cache Enables the configuration cache. Gradle will try to reuse the build configuration from previous builds. [incubating] --configuration-cache-problems Configures how the configuration cache handles problems (fail or warn). Defaults to fail. [incubating] --configure-on-demand Configure necessary projects only. Gradle will attempt to reduce configuration time for large multi-project builds. [incubating] --console Specifies which type of console output to generate. Values are 'plain', 'auto' (default), 'rich' or 'verbose'. --continue Continue task execution after a task failure. -D, --system-prop Set system property of the JVM (e.g. -Dmyprop=myvalue). -d, --debug Log in debug mode (includes normal stacktrace). --daemon Uses the Gradle daemon to run the build. Starts the daemon if not running. --export-keys Exports the public keys used for dependency verification. -F, --dependency-verification Configures the dependency verification mode. Values are 'strict', 'lenient' or 'off'. --foreground Starts the Gradle daemon in the foreground. -g, --gradle-user-home Specifies the Gradle user home directory. Defaults to ~/.gradle -I, --init-script Specify an initialization script. -i, --info Set log level to info. --include-build Include the specified build in the composite. -M, --write-verification-metadata Generates checksums for dependencies used in the project (comma-separated list) -m, --dry-run Run the builds with all task actions disabled. --max-workers Configure the number of concurrent workers Gradle is allowed to use. --no-build-cache Disables the Gradle build cache. --no-configuration-cache Disables the configuration cache. [incubating] --no-configure-on-demand Disables the use of configuration on demand. [incubating] --no-daemon Do not use the Gradle daemon to run the build. Useful occasionally if you have configured Gradle to always run with the daemon by default. --no-parallel Disables parallel execution to build projects. --no-scan Disables the creation of a build scan. For more information about build scans, please visit https://gradle.com/build-scans. --no-watch-fs Disables watching the file system. --offline Execute the build without accessing network resources. -P, --project-prop Set project property for the build script (e.g. -Pmyprop=myvalue). -p, --project-dir Specifies the start directory for Gradle. Defaults to current directory. --parallel Build projects in parallel. Gradle will attempt to determine the optimal number of executor threads to use. --priority Specifies the scheduling priority for the Gradle daemon and all processes launched by it. Values are 'normal' (default) or 'low' --profile Profile build execution time and generates a report in the <build_dir>/reports/profile directory. --project-cache-dir Specify the project-specific cache directory. Defaults to .gradle in the root project directory. -q, --quiet Log errors only. --refresh-dependencies Refresh the state of dependencies. --refresh-keys Refresh the public keys used for dependency verification. --rerun-tasks Ignore previously cached task results. -S, --full-stacktrace Print out the full (very verbose) stacktrace for all exceptions. -s, --stacktrace Print out the stacktrace for all exceptions. --scan Creates a build scan. Gradle will emit a warning if the build scan plugin has not been applied. (https://gradle.com/build-scans) --status Shows status of running and recently stopped Gradle daemon(s). --stop Stops the Gradle daemon if it is running. -t, --continuous Enables continuous build. Gradle does not exit and will re-execute tasks when task file inputs change. --update-locks Perform a partial update of the dependency lock, letting passed in module notations change version. [incubating] -V, --show-version Print version info and continue. -v, --version Print version info and exit. -w, --warn Set log level to warn. --warning-mode Specifies which mode of warnings to generate. Values are 'all', 'fail', 'summary'(default) or 'none' --watch-fs Enables watching the file system for changes, allowing data about the file system to be re-used for the next build. --write-locks Persists dependency resolution for locked configurations, ignoring existing locking information if it exists -x, --exclude-task Specify a task to be excluded from execution. -- Signals the end of built-in options. Gradle parses subsequent parameters as only tasks or task options.
最新发布
08-02
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值