Logger


Source link: https://github.com/sujithkanna/logger

Logger

A simple log helper which helps you to log in a whole different level - You can enable and disable logs - Can print method names when necessary ##Include library Including logger is very simple

compile 'com.github.sujithkanna:logger:0.4'

add this to your dependencies in build.gradle. ##Import

import com.hsalf.logger.Log;

import the log from this package instead of default log.

Basics

Prints the message like the android default log does.

 Log.e(...);
 = Error  Log.v(...);
 = Verbose  Log.d(...);
 = Debug  Log.i(...);
 = Info  Log.w(...);
 = Warning 

##Enabling/Disabling To enable logs

Log.enable()

To disable logs

Log.disable();

Note: The speciality of this is internally it wont check each and every time using a boolean to detect if the log is enabled, instead it switches between modes. Basically there are two instacnes of log inside this Log.java file, One is empty and other is live. If you enable the logs, the live one will be used to print logs, if you disable logs, then the live log instance will be replaced with empty log instance which prints nothing. This way we can avoid unnecessary checks before printing logs. ##Logging

Log.i(TAG, "Hello log");

you can log anything you want.

Log.i(TAG, 12345);
 Log.i(TAG, jsonObject);
 Log.i(TAG, yourCustomPojoClass);

To print your own pojo class you have to override the toString method inside your class

public static class MyPojo {

  private String name;
  private int age;
  @Override
  public String toString() {

return "Mypojo{
" +

  "name='" + name + '\'' +

  ", age=" + age +

  '
}
';
  
}
 
}
 

Log with throwable

try {
  // Your code which might throw error 
}
 catch (Exception ex) {
  Log.e(TAG, "Got an internal error", ex) 
}

Like the default android log you can log messages with throwables.

Features

Now you can print logs with auto tags which is class and method names and the line number. if you are calling the log from your MainActivity's onCreate

private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.activity_main);

  Log.e(TAG, "test hello log", true);
 
}

This will print like

E/MainActivity_onCreate()_16: test hello log

Here the "MainActivity" is the tag you gave, "onCreate()" is the method where you called the log from, and "16" is the line numer.

The auto naming method is little bit expensive, so it is highly recommended to use this only in debug mode. But is is very simple to enable/disable auto naming. ##Auto Naming ####Create an application class for your application

public class MyApplication extends Application {

private static final String TAG = "MyApplication";

private static MyApplication sMyApplication;

@Override
  public void onCreate() {

super.onCreate();

sMyApplication = this;

boolean debug = Log.isDebuggable(this);

Log.enableAutoNameDetection(debug);
 // Highly recommended

// This enables the auto naming only in debug mode.
  
}

public static MyApplication getInstance() {

return sMyApplication;
  
}
 
}

mention it in your manifest's application tag

<application

android:name=".MyApplication" // Your application class here

...>
// Your activities, receivers and services </application>

Log (Auto naming)

if you call log from your main activity like this

Note: if enableAutoNameDetection() is set to true

Log.v("My message");

it will print the log as

V/MainActivity_myMethod()_22: My message

Here the "MainActivity" is the class name and "onCreate()" is the method where you called the log from, and 22" is the line numer.

Note: if enableAutoNameDetection() is set to false

Log.v("My message");

it will print the log as

V/Log: My message

Since auto name detection is disabled, it wont auto detect the class name, so the tag by default set to "Log" instead of class name. That is why giving the tag is highly recommended.

Log.v("MainActivity", "My message");

Now we have given the tag name manually, So even if the auto name detection is disabled, the log will be printed as

V/MainActivity: My message

Now you may need auto method naming in release build also. So here is a way to use auto naming which wont affect app performance on release mode.

Note: For the following procedures you should not set Log.enableAutoNameDetection() to false, by default it is true

Create a boolean member variable in your applcation class as "debuggable;

private static MyApplication sMyApplication;  private boolean debuggable; @Override public void onCreate() {

  super.onCreate();

  sMyApplication = this;
  debuggable = Log.isDebuggable(this);
 
}
  public static MyApplication getInstance() {

return sMyApplication; 
}
  public boolean isDebbagable() {
  return debuggable; 
}

Now use call the "isDebuggable" method to auto detect method name while printing.

Log.v("MainActivity", "My message", MyApplication.getInstacne().isDebuggalbe());

The method name will be auto detected only if the app is in debug mode.

now you can tell your application to auto detect method name eve in release mode by calling log with true as last param

Log.v("MainActivity", "My message", true);

If you know that you always need the class name ad method name even in release mode, then use

Log.v("Your log message");

Note: It is highly recommended not to use auto detection in frequent places like loops, inputstream reads etc...

Resources

An implementation of Cassowary, a linear constraint based layout manager for Android.

Adapter and LayoutManager for Android RecyclerView which enables sticky header positioning.

A Library to show Round ImageView with Face Detection and text labels on right side. Best suited for person details.

OAuth RxJava extension for Android.

This library helps to display overlapping views (topmost) in order to show some kind of message to capture user attention. It can be an error, success message, loading bar or just simple text message.

This is a set of classes with a small, extensible API which provides basic abstractions for building your apps using VIPER architecture.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes