Skip to content

amiranmanesh/pulp

 
 

Repository files navigation

Pulp

Pulp is a very simple logger tool for android using Kotlin programming language.

Installation

  • Add the dependency code to your project
implementation "ir.malv.utils:pulp:$version"

Or include the pre-release version

  • Add this to your gradle repositories
maven { url 'https://dl.bintray.com/mah-d/preview' }
implementation "ir.malv.utils:pulp:$version"

Usage

Simple usage

Pulp.info("TAG", "This is a message")

Add extra data

Pulp.info("TAG", "Message, but not enough") {
   "ExtraMessage1" to "Message..."
   "ExtraMessage2" to "Message..."
   // ...
}

Note: You can pass an array of tags instead of one:

Pulp.warn(arrayOf("Be careful", "MyApp"), "Message", throwable)

Output will be like:

2019-08-01 16:06:40.768 26299-26299/ir.malv.logtest I/Pulp: MainTag:
    Tags: [TAG]
    Message: Message but not enough
    Data:
     ExtraMessage1  Message...
     ExtraMessage2  Message...

Add throwable to log

val t = Throwable("Error")
Pulp.error("TAG", "Failed", t) {
    "Extra data" to "data"
}

Setting the Log tag

Pulp.setMainTag("My APP")

Advanced usage

Toggle Enable/Disable Pulp

Pulp.setLogsEnabled(false)

Disabling the logger can also be done using an AndroidManifest meta-data.

<meta-data android:name="pulp_enabled" android:value="false" />

Note: To get Pulp extract manifest (since it needs constructor), you need to add this to your Application class:

Pulp.init(this /* context */)

Listening to Logs when it was triggered

When Pulp triggers to print a log, it can be listened using callbacks.

Pulp.addHandler(object: Pulp.LogHandler {
    override fun onLog(
      level: Pulp.Level,
      tags: List<String>,
      message: String,
      t: Throwable?,
      data: Pulp.LogData
    ) {
        // Get the data and do stuff
    }
})

Note: You can add multiple handlers and all of them will be called when logging.

  • Handlers can also be disabled:
Pulp.setHandlerEnabled(enabled = false)

Enable using database

Pulp can save logs into it's database and return the in a LiveData stream. To enable database call:

Pulp.setDatabaseEnabled(true)

And because interacting with database needs context, make sure you have called Pulp.init(context), or Pulp.setApplicationContext(context).

And to interact with database using this code:

val savedLogs: LiveData<List<PulpItems>> = Pulp.getSavedLogs(context)

savedLogs.observe(activity, Observer {
   // it is List<PulpItem>
})

and to clear the logs:

Pulp.clearLogs(context)
Log levels
  • D: Debug
  • I: Info
  • W: Warning
  • E: Error
  • WTF: Unexpected

Print a simple message (not a Pulp log)

When you don't want to send callback or follow the message style of pulp, you can print a simple message.

Pulp.sout("Message") // output: MainTag ### Message

It will not notify callbacks.

  • All config methods can be chained:
Pulp.init(this)
    .setMainTag("MyApp")
    .setHandlerEnabled(true)
    .setLogsEnabled(true)
    .setDatabaseEnabled(true)
    .addHandler(object : Pulp.LogHandler {
    // ...
})

About

Very lightweight logger library for Android using Kotlin

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Kotlin 97.1%
  • Java 2.9%