项目作者: Chesire

项目描述 :
Android library to log out Activity and Fragment lifecycle methods
高级语言: Kotlin
项目地址: git://github.com/Chesire/LifecykleLog.git
创建时间: 2019-03-22T21:14:00Z
项目社区:https://github.com/Chesire/LifecykleLog

开源协议:Apache License 2.0

下载


LifecykleLog

Library to easily log out Android lifecycle methods for Activities and Fragments.

Android Arsenal
Android CI
codecov

Installation

Gradle:
Make sure you have JitPack as a source

  1. maven { url 'https://jitpack.io' }

add the following line to your build.gradle, with version being the latest tag.

  1. implementation 'com.github.chesire:lifecyklelog:{version}'

Usage example

Initialize in your application class.

  1. class ApplicationOverride : Application() {
  2. override fun onCreate() {
  3. super.onCreate()
  4. LifecykleLog.initialize(this)
  5. }
  6. }

Add the @LogLifecykle annotation to the Activity or Fragment that the
lifecycle methods should be logged for.

  1. @LogLifecykle
  2. class MainActivity : AppCompatActivity() { ...
  3. @LogLifecykle
  4. class MainFragment : Fragment() { ...

Then lifecycle events will be logged out in logcat.

  1. D/Lifecykle: MainActivity onStart
  2. D/Lifecykle: MainFragment onAttach
  3. D/Lifecykle: MainFragment onCreate
  4. D/Lifecykle: MainFragment onCreateView
  5. D/Lifecykle: MainFragment onActivityCreated
  6. D/Lifecykle: MainFragment onStart
  7. D/Lifecykle: MainActivity onResume
  8. D/Lifecykle: MainFragment onResume
  9. D/Lifecykle: MainActivity onPause
  10. D/Lifecykle: MainFragment onPause
  11. D/Lifecykle: MainActivity onStop
  12. D/Lifecykle: MainFragment onStop

Configuration

Logging mechanism

By default LogLifecykle will output to Log.d with a tag of Lifecykle, to
override this behaviour pass an implementation into the
LifecykleLog.logHandler.

  1. LifecykleLog.logHandler = LogHandler { clazz, lifecycleEvent, bundle ->
  2. Log.e(clazz, lifecycleEvent)
  3. }

This can allow you to use other logging frameworks such as Timber.

  1. LifecykleLog.logHandler = LogHandler { clazz, lifecycleEvent, bundle ->
  2. Timber.i("$clazz -> $lifecycleEvent - $bundle")
  3. }

For lifecycle methods which pass a bundle along, it will automatically be pushed
through the LogHandler. In instances where there is no bundle, or it is empty,
then the value will simply be “null”.

Lifecycle methods

To customise which lifecycle methods are logged out, an array of the
LifecycleEvent enum can be passed into LifecykleLog.logEvents, this can
also be done with the @LogLifecykle annotation.

  1. LifecykleLog.logEvents = arrayOf(
  2. LifecycleEvent.ON_CREATE,
  3. LifecycleEvent.ON_DESTROY
  4. )
  5. @LogLifecykle(overrideLogEvents = [LifecycleEvent.ON_START])
  6. class MainActivity : AppCompatActivity() {
  7. @LogLifecykle(overrideLogEvents = [LifecycleEvent.ON_ACTIVITY_CREATED, LifecycleEvent.ON_ATTACH])
  8. class MainFragment : Fragment() {

If logEvents is provided to the LifecykleLog then it will override the
defaults.
If overrideLogEvents is provided on the annotation, only the methods that
are provided in this will be logged out.

Class name

To customise the class name that is logged out, a new name can be provided to
the annotation.

  1. @LogLifecykle(className = "MainActivity")
  2. class MainActivity : AppCompatActivity() {
  3. @LogLifecykle(className = "MaybeMainFragment")
  4. class MainFragment : Fragment() {

This can be useful if ProGuard strips out the class names and you really need to
see them in the logs. By default the name will be pulled from the objects
class.java.simpleName.

Remove Annotation

If you want to perform logging of ALL Activities and Fragments, without
needing to add the annotation to them, then the configuration option
requireAnnotation can be set to false.

  1. LifecykleLog.requireAnnotation = false

This will ignore any annotations that are currently set, and perform logging for
every Activity and Fragment on the lifecycle events defined in
LifecykleLog.logEvents.

For more examples and usage, please refer to the
sample.

Contributing

Please read
CONTRIBUTING.md
for details on how to contribute.

License

Apache 2.0 - See
LICENSE for more
information.