木材切割优化
In this article, I am gonna show you how you can get logs of your app on top of the Android default logs using Timber.
在本文中,我将向您展示如何使用Timber在Android默认日志之上获取应用日志。
In your app, use the following syntax to get the logs.
在您的应用程序中,使用以下语法获取日志。
In the default way, you always need to write TAG which could be anything like the name of your activity. But in timber, TAG will be defined by the library itself.
默认情况下,您始终需要编写TAG ,该名称可能类似于您的活动名称。 但是在木材中, TAG将由库本身定义。
In order to integrate timber into your app, it must be initialized when the app starts. So you need to create a base application class.
为了将木材集成到您的应用程序中,必须在应用程序启动时将其初始化。 因此,您需要创建一个基础应用程序类。
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); if(BuildConfig.DEBUG){ Timber.plant(new Timber.DebugTree()); } } }Initlizied Timber in onCreate()
onCreate()初始化木材
Timber.DebugTree() print logs in debug mode.
Timber.DebugTree()以调试模式打印日志。
You can also add logs in the release mode of your app by just adding Timber.ReleaseTree() in onCreate().
您还可以通过在onCreate()添加Timber.ReleaseTree()在应用的发布模式中添加日志。
Finally, it’s time to add this application class in AndroidManifest so that when the app launches, the application class will initialize first.
最后,是时候在AndroidManifest添加此应用程序类,以便在应用程序启动时,该应用程序类将首先进行初始化。
<application android:name=".MyApplication" // <----- defining application class android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>Now let’s head over to our MainActivity.java class and see the magic of Timber. I am just making it simple for learning purposes. You can add timber in your complex apps but for now, I am just getting logs using android lifecycle methods.
现在,让我们转到MainActivity.java类,看看Timber的魔力。 我只是为了学习目的而使其简单。 您可以在复杂的应用程序中添加木材,但就目前而言,我只是使用android生命周期方法获取日志。
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Timber.d("onCreate Called"); } @Override protected void onResume() { super.onResume(); Timber.d("OnResume Called"); } @Override protected void onPause() { super.onPause(); Timber.d("onPause Called"); } @Override protected void onStart() { super.onStart(); Timber.d("onStart Called"); } @Override protected void onStop() { super.onStop(); Timber.d("onStop Called"); } @Override protected void onDestroy() { super.onDestroy(); Timber.d("onDestroy called"); } }Let’s run the app and see what we will get in logcat.
让我们运行该应用程序,看看我们将在logcat获得什么。
You can see clearly the D/MainActivity: has added by the library itself as a TAG so that you will easily find the logs with respect to your activities.
您可以清楚地看到D/MainActivity:由库本身添加为TAG以便您可以轻松地找到有关您的活动的日志。
Found this article useful? Check out my most read articles below:
觉得这篇文章有用吗? 在下面查看我最常阅读的文章:
Want to become a Data Scientist? Follow this roadmap
想要成为数据科学家? 遵循此路线图
Want to Become a Successful Android Developer? Follow this roadmap
想要成为一名成功的Android开发人员? 遵循此路线图
Full-Screen Bottom Sheet
全屏底页
How to make RecyclerView item expandable?
如何使RecyclerView项目可扩展?
Want to make Animated Floating Action Button with More Option?
想要使Animated Floating Action Button具有更多选项吗?
Make RecyclerView With beautiful Animations!
用漂亮的动画制作RecyclerView!
翻译自: https://levelup.gitconnected.com/timber-a-logging-library-for-android-56c431cd7300
木材切割优化
相关资源:微信小程序源码-合集6.rar