Code Monkey home page Code Monkey logo

klog's Introduction

Klog - A Kotlin Log lib

简介 (what is Klog?)

Klog是用Kotlin所写的,利用其类扩展,实现了链式调用时,在不破坏其代码整体性的情况下,能够实现数据打印跟踪的log库.

  • 优点:
    1.链式打印
    2.自动识别调用方类名为TAG
    3.打印线程号,代码行 (默认不开启,需要开启请 Klog.getSettings().setBorderEnable(true))
  • 缺点:
    由于Kotlin对于伴生类方法的惰性加载优化, 在kt中调用可以使用 Klog.d("hello"),但是在java中,其使用的是内部类的方式实现惰性加载.
    因此在java中, 调用方式变为了 Klog.Companion.i("test");

有更好的实现方式的欢迎pr or issues

Download

project's build.gradle

  	allprojects {
  		repositories {
              ...
              maven { url 'https://jitpack.io' }
  		}
  	}

module's build.gradle (模块的build.gradle)

	dependencies {
	        compile 'com.github.Jerey-Jobs:Klog:v0.2'
	}

或者, 您可以直接将Klog.kt文件拷贝至您的项目(推荐)

说明(Explanation)

无论什么语言, 程序员都需要输出log, Kotlin的语法扩展功能可为我们带来更新奇的输出log模式.

输出log的目的,无非两种情况
1.用来表明程序执行到哪了
2.打印数据处理前后的值

那么Klog带来了数据边处理边打印的模式.

比如这种边处理字符串边打印的模式.

    str.log()                   //直接输出该对象toString
       .toUpperCase()
       .log("upper")             //输出带提示的处理结果
       .toLowerCase()            //继续处理
       .log("lower")

又比如在lambda时,我们调试时需要跟踪数据被处理的情况

    var list = arrayListOf<String>("aaa", "bb", "cccc", "ddddd")
    list.log("init")
        .map { it -> it.toUpperCase() }
        .log("after map")
        .filter { it -> it.length > 2 }
        .log("after filter")

又或者,我们需要直接打印

    //修改Klog设置, 开启边框打印
    Klog.getSettings()
            .setBorderEnable(true)

    Klog.a("aaaaaaa")            //普通log输出方式1
    Klog.a(contents = "bbbbb")   //普通log输出方式2
    Klog.i("jerey", "aaaaaaa")    //带tag输出

so, Kotlin的类扩展为我们带来无限遐想

License

Copyright 2017 Jerey-Jobs.

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.

klog's People

Contributors

jerey-jobs avatar android-knight avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.