# Kotlin logging extensions for Timber
Jake Wharton's [Timber](https://github.com/JakeWharton/timber) library is great. It's a Java library with an API that works well for Java, but that isn't as idiomatic when used in Kotlin.
This library builds on Timber with an API that's easier to use from Kotlin. Instead of using formatting parameters, you pass a lambda that is only evaluated if the message is logged.
## Usage
1. Configure any `Tree` instances in your Application's `onCreate`, the same way as with plain [Timber](https://github.com/JakeWharton/timber#usage).
2. Call the extension functions from anywhere in your code.
```kotlin
// Standard timber
Timber.d("%d %s", intVar + 3, stringFun())
// Kotlin extensions
Timber.d { "${intVar + 3} ${stringFun()}" }
// or
d { "${intVar + 3} ${stringFun()}" }
```
The same message and tags will be logged in all three cases.
The Kotlin extensions have the advantage of being more convenient to write, and are also more performant in some circumstances. The passed block is only evaluated if the message is logged, and even if the message is logged to multiple trees, the block is only evaluated once. All extension methods are inlined, so there is no method count penalty to using this library.
Logging exception objects works the same way:
```kotlin
// Standard timber
Timber.e(exception, "%d exceptions", errorCount)
// Kotlin extensions
Timber.e(exception) { "$errorCount exceptions" }
// or
e(exception) { "$errorCount exceptions" }
```
## What about Timber's custom lint checks?
Timber comes with half a dozen lint checks that help you spot incorrect usage of the log calls.
With the exception of long custom tags, none of the errors those checks look for are possible with this library. You can perform arbitrary code inside of the lambdas passed to the log extensions, and there's no risk of performance problems in your release code since the blocks won't be evaluated unless the messages are printed.
## Download
The Kotlin extensions for Timber are distributed with Maven Central,
[JCenter](https://bintray.com/ajalt/maven/timberkt) and
[JitPack](https://jitpack.io/#ajalt/timberkt/1.5.1).
```groovy
implementation 'com.github.ajalt:timberkt:1.5.1'
```
## Documentation
[The documentation is hosted online here](https://jitpack.io/com/github/ajalt/timberkt/1.5.1/javadoc/timberkt/com.github.ajalt.timberkt/index.html).
## License
```
Copyright 2017-2018 AJ Alt
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
```
Android-使用Kotlin和Timber轻松实现Android日志记录
需积分: 50 31 浏览量
2019-08-13
02:34:14
上传
评论
收藏 100KB ZIP 举报
weixin_39840650
- 粉丝: 409
- 资源: 1万+
最新资源
- Jurassic Pack Vol. II Dinosaurs 侏罗纪包卷恐龙二号Unity游戏模型资源unitypackage
- Jurassic Pack Vol. III Dinosaurs 侏罗纪包卷恐龙三号Unity游戏模型资源unitypackag
- Ultimate Seating Controller 终极座椅控制器Unity游戏开发插件资源unitypackage
- 什么是人工智能-关于人工智能的相关介绍说明
- Figma Converter for Unity适用Unity的Figma转换器Unity游戏开发插件unitypackage
- Creepy Animatronic Anims 令人毛骨悚然的电子动画Unity游戏动画插件资源unitypackage
- Rankings & Leaderboards 排名和排行榜Unity游戏开发插件资源unitypackage
- Semantic Color Palette 语义调色板Unity游戏开发插件资源unitypackage
- Low Poly Nature:Lush and Diverse Environments低聚自然郁郁Unity低多边形模型资源
- voc数据集是什么-我们如何使用voc数据集
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈