Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including react-native-android-log with all npm packages installed. Try it out:

require("react-native/package.json"); // react-native is a peer dependency. var reactNativeAndroidLog = require("react-native-android-log")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

react-native-android-log v1.1.0

Easy and useful log methods for Android apps in debug or release mode, with minimal impact

react-native-android-log

npm License

Easy and useful log methods for Android apps in debug or release mode, with minimal impact.

Works with RN 0.50+, Gradle plugin 3.1.x and Gradle 4.4 or later.

Default build is for minSdkVersion 21 and targetSdkVersion 27, but you can configure this.

NOTE:

In iOS, the default level is SUPPRESS, which disables the output. If you change it, console.log will be used.

Install

  1. Install the module from npm and link it:

    $ yarn add react-native-android-log
    $ react-native link react-native-android-log
    
  2. Replace 'compile' with 'implementation' in your android/app/build.gradle. It must looks like:

    implementation project(':react-native-android-log')
    

For VS Code users

Optional: You can add the tag used by Log to the logCatArguments properties in .vscode/launch.json

This example will include React Native warnings and errors, and all the messages from App in the "OUTPUT" panel:

    ...
    {
      "name": "Debug Android",
      "program": "${workspaceRoot}/.vscode/launchReactNative.js",
      "type": "reactnative",
      "request": "launch",
      "platform": "android",
      "sourceMaps": true,
      "outDir": "${workspaceRoot}/.vscode/.react",
      "logCatArguments": [
        "*:S",
        "ReactNative:W",
        "ReactNativeJS:W",
        "App:V"
      ]
    },

SDK version Settings

Optional: In your android/build.gradle file, set the SDK versions that your app is using.

Example with the predefined values:

ext {
    def buildToolsVersion  = '27.0.3'
    def minSdkVersion      = 16
    def compileSdkVersion  = 27
    def targetSdkVersion   = 27
}

Usage

Log messages in your JavaScript files:

import Log from 'react-native-android-log'

// Set the default priority level (optional)
Log.setLevel(__DEV__ ? Log.VERBOSE : Log.WARN)

...
Log.v('Verbose message')    // no output in release builds
Log.w('Debugging')

// debugging message with amother tag:
Log.d('Proc2', 'warning')

...and see the output in the console through adb:

$ adb logcat -s App:V Proc2:V

or in the OUPUT panel of VS Code, if you are using the React Native Tools extension.

API

Contstants

Numeric levels, from most to least verbosity:

ConstantDescription
VERBOSEUsed by Log.v, outputs nothing unless you use Log.setLevel(Log.VERBOSE)
DEBUGPriority constant for Log.d
INFOPriority constant for Log.i
WARNPriority constant for Log.w
ERRORPriority constant for Log.e
SUPPRESSThis is a special constant used to disable logging

Methods

  • setTag(tag: string)

    Sets the tag to use by the logging methods without a tag parameter.

    The predefined tag is "App".

  • setLevel(tag: string | null, level: number)

    Allows you to change the logging level for a specific tag or, if you pass a null tag, the default level for all the tags.

    The predefined level is DEBUG for debug builds and ERROR for release builds.

  • getLevel(tag?: string | null)

    Return the level for a specific tag.

    If tag is null, not specified, or does not have a specific level, this method return the default level for all the tags.

  • print(level: string, tag: string, msg: string)

  • print(level: string, msg: string)

    Base method for all the one-letter logging methods.

  • v (VERBOSE)

  • d (DEBUG)

  • i (INFO)

  • w (WARN)

  • e (ERROR)

    The above methods are one-letter shortcuts to the print method with the corresponding priority level and can be invoked with or without the tag parameter:

    method(tag: string, msg: string)

    method(msg: string)

    If you omit the tag, the one specified with setTag will be used, or the predefined tag "App".

License

MIT © 2018 Alberto Martínez

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free