AndroidLogger


Source link: https://github.com/danylovolokh/AndroidLogger

AndroidLogger

This is a lightweight Logger to file for Android. It's based on VoNaLogger

Recommended usage

  • Add this to your build.gradle file of the project.
buildscript {

  repositories {

jcenter()
  
}
 
}
 
  • Add this dependency.
dependencies {

  compile 'com.github.danylovolokh:android-logger:1.0.2' 
}
 
  • Initalize AndroidLogger in your Application class before using it.


  File logsDirectory = AndroidLogger.getDefaultLogFilesDirectory(this);

int logFileMaxSizeBytes = 2 * 1024 * 1024; // 2Mb

try {

 AndroidLogger.initialize(

this,

logsDirectory,

"Log_File_Name",

logFileMaxSizeBytes,

false

);

}
 catch (IOException e) {

 // Some error happened - most likely there is no free space on the system

}
 

To log text to file use AndroidLogger like this.



  AndroidLogger.v("TAG", "Verbose Message");

AndroidLogger.d("TAG", "Debug Message");

AndroidLogger.i("TAG", "Info Message");

AndroidLogger.w("TAG", "Warn Message");

AndroidLogger.e("TAG", "Error Message");
 

NOTE: this will not log the message to LogCat! If you want to log to both LogCat and File you need to create some kind of LoggerWrapper, like this.

public abstract class LoggerWrapper {

 public static int v(final String TAG, final String message) {

Log.v(TAG, message);

return AndroidLogger.v(TAG, message);

  
}

public static int d(final String TAG, final String message) {

Log.d(TAG, message);

return AndroidLogger.d(TAG, message);

  
}

public static int inf(final String TAG, final String message) {

Log.i(TAG, message);

return AndroidLogger.i(TAG, message);

  
}

public static int w(final String TAG, final String message) {

Log.w(TAG, message);

return AndroidLogger.w(TAG, message);

  
}

public static int err(final String TAG, final String message) {

Log.e(TAG, message);

return AndroidLogger.e(TAG, message);

  
}
 
}
 
  • How to get logs. I recommend to use this method:

 // after calling this method AndroidLoger stops receiving new logs so you will need to reinitialize it.
  AndroidLogger.processPendingLogsStopAndGetLogFiles(new AndroidLogger.GetFilesCallback() {

@Override

public void onFiles(File[] logFiles) {

 // get everything you need from these files

 try {

  AndroidLogger.reinitAndroidLogger();

 
}
 catch (IOException e) {

  e.printStackTrace();

 
}

}

  
}
);
 

There are also other options to get logs from file. Please see the source code.

How it works.

Here is a decription from VoNaLogger. Because basically AndroidLogger is jsut a wrapper. The logs (any parameters passed to the logger) are stored in Log Entries. Log Entries are reused because the main goal of this library is to create the smallest amount of objects during writing to file.

License

Copyright 2017 Danylo Volokh

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.

Resources

This library has been built in order to provide a Reactive wrapper around the Android Network Service Discovery API and the JmDNS implementation for older devices.

A tiny Android library that makes a very simple horizontal ProgressBar with animation.

Android custom button with centered drawable.

AndroidRPInterpolator is the interpolator library using Rovert Penner's Easing Functions.

Android CheckBox and RadioButton with additional 3rd 'indeterminate' state.

Context free and basic utils to build Android project conveniently.

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