Skip to content

Instantly share code, notes, and snippets.

@webserveis
Last active March 31, 2020 13:45
Show Gist options
  • Save webserveis/09ae388bba6665cef721ad28301ac646 to your computer and use it in GitHub Desktop.
Save webserveis/09ae388bba6665cef721ad28301ac646 to your computer and use it in GitHub Desktop.
Live template Android Log Kotlin

Si queremos añadir las plantillas en vivo (LiveTemplates) para facilitar el uso del Log

Instalación

  1. Añadir el AndroidLogKotlin.xml en el directorio Windows: <user_home>.AndroidStudio\config\templates Linux: ~/.AndroidStudio/config/templates macOS: ~/Library/Preferences/Android Studio/templates

  2. Abrir el Android Studio, ir en Settings->Live Template, comprobar la existencia del grupo AndroidLogKotlin

Su uso

https://i.stack.imgur.com/SeU8z.gif

<templateSet group="AndroidLogKotlin">
<template name="logm" value="android.util.Log.d(TAG, $FORMAT$)" description="Log method name and its arguments" toReformat="true" toShortenFQNames="true">
<variable name="FORMAT" expression="groovyScript(&quot;def params = _2.collect {it + ' = [$' + it + ']'}.join(', '); return '\&quot;' + _1 + '() called' + (params.empty ? '' : ' with: ' + params) + '\&quot;'&quot;, kotlinFunctionName(), functionParameters())" defaultValue="" alwaysStopAt="false" />
<context>
<option name="KOTLIN_STATEMENT" value="true" />
</context>
</template>
<template name="logd" value="android.util.Log.d(TAG, &quot;$METHOD_NAME$: $content$&quot;)" description="Log.d(String)" toReformat="true" toShortenFQNames="true">
<variable name="METHOD_NAME" expression="kotlinFunctionName()" defaultValue="" alwaysStopAt="false" />
<variable name="content" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="KOTLIN_STATEMENT" value="true" />
</context>
</template>
<template name="loge" value="android.util.Log.e(TAG, &quot;$METHOD_NAME$: $content$&quot;, $exception$)" description="Log.e(Exception, String)" toReformat="true" toShortenFQNames="true">
<variable name="METHOD_NAME" expression="kotlinFunctionName()" defaultValue="" alwaysStopAt="false" />
<variable name="content" expression="" defaultValue="" alwaysStopAt="true" />
<variable name="exception" expression="" defaultValue="e" alwaysStopAt="true" />
<context>
<option name="KOTLIN_STATEMENT" value="true" />
</context>
</template>
<template name="logi" value="android.util.Log.i(TAG, &quot;$METHOD_NAME$: $content$&quot;)" description="Log.i(String)" toReformat="true" toShortenFQNames="true">
<variable name="METHOD_NAME" expression="kotlinFunctionName()" defaultValue="" alwaysStopAt="false" />
<variable name="content" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="KOTLIN_STATEMENT" value="true" />
</context>
</template>
<template name="logw" value="android.util.Log.w(TAG, &quot;$METHOD_NAME$: $content$&quot;)" description="Log.w(Exception, String)" toReformat="true" toShortenFQNames="true">
<variable name="METHOD_NAME" expression="kotlinFunctionName()" defaultValue="" alwaysStopAt="false" />
<variable name="content" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="KOTLIN_STATEMENT" value="true" />
</context>
</template>
<template name="logwtf" value="android.util.Log.wtf(TAG, &quot;$METHOD_NAME$: $content$&quot;)" description="Log.wtf(Exception, String)" toReformat="true" toShortenFQNames="true">
<variable name="METHOD_NAME" expression="kotlinFunctionName()" defaultValue="" alwaysStopAt="false" />
<variable name="content" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="KOTLIN_STATEMENT" value="true" />
</context>
</template>
<template name="logt" value="private val TAG = $NAME$::class.java.simpleName" description="A static logtag with your current classname" toReformat="true" toShortenFQNames="true">
<variable name="NAME" expression="fileNameWithoutExtension()" defaultValue="" alwaysStopAt="false" />
<context>
<option name="KOTLIN" value="true" />
</context>
</template>
</templateSet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment