Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions log4j-samples-android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties
1 change: 1 addition & 0 deletions log4j-samples-android/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
48 changes: 48 additions & 0 deletions log4j-samples-android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
plugins {
id 'com.android.application'
}

android {
compileSdk 34

defaultConfig {
applicationId "org.apache.logging.log4j.samples.android"
minSdk 26
targetSdk 34
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

packagingOptions {
resources.excludes.add("META-INF/*")
resources.excludes.add("serviceAccountKey*.json")
}
}

dependencies {

implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.11.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'

//Log4j
implementation 'org.apache.logging.log4j:log4j-api:2.24.1'
implementation 'org.apache.logging.log4j:log4j-core:2.24.1'
}
21 changes: 21 additions & 0 deletions log4j-samples-android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.log4japi;

import android.content.Context;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.example.log4japi", appContext.getPackageName());
}
}
27 changes: 27 additions & 0 deletions log4j-samples-android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.apache.logging.log4j.samples.android">

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Log4jAPI"
tools:targetApi="31">
<activity
android:name="MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
21 changes: 21 additions & 0 deletions log4j-samples-android/app/src/main/assets/log4j2.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Root logger configuration
status = error
name = PropertiesConfig

# Appenders
appender.console.type = Console
appender.console.name = Console
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%d{yyyy-MM-dd HH:mm:ss.SSS}] %-5p- %m%n

# Root logger level and appenders
rootLogger.level = INFO
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = Console

logger.com.example.log4japi.MainActivity.name = com.example.log4japi.MainActivity
logger.com.example.log4japi.MainActivity.level = DEBUG
logger.com.example.log4japi.MainActivity.additivity = false
logger.com.example.log4japi.MainActivity.appenderRefs = stdout
logger.com.example.log4japi.MainActivity.appenderRef.stdout.ref = Console
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.apache.logging.log4j.samples.android;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.Configurator;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

private int logLevelIdx;

//Logger log = LogManager.getLogger(MainActivity.class);
Logger log = LogManager.getRootLogger();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

/*// Manually read in log4j2.properties
LoggerContext context = (LoggerContext) LogManager.getContext(false);
try {
InputStream inputStream = getAssets().open("log4j2.properties");
ConfigurationSource source = new ConfigurationSource(inputStream);
context.start(ConfigurationFactory.getInstance().getConfiguration(context, source));
} catch (IOException e) {
e.printStackTrace();
}*/

/*// Explicitly set the logging level for this logger to INFO
//Configurator.setLevel(log, Level.INFO);
LoggerConfig loggerConfig = context.getConfiguration().getLoggerConfig(log.getName());
loggerConfig.setLevel(Level.INFO);*/

//
// UI bindings
//
//TextView displaying Log Level
TextView logLevelTxt = findViewById(R.id.logLevelTxt);
logLevelTxt.setText(log.getLevel().name());

//Change log level
Button setLogLevelBtn = findViewById(R.id.setLogLevelBtn);
setLogLevelBtn.setOnClickListener(v -> {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("Set log level");

String[] logLevels = new String[]{"OFF", "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE", "ALL"};
logLevelIdx = Arrays.asList(logLevels).indexOf(log.getLevel().name());

alertDialog.setSingleChoiceItems(logLevels, logLevelIdx, (dialog, which) -> logLevelIdx = which);
alertDialog.setPositiveButton("Select", (dialog, which) -> {
Configurator.setLevel(log, Level.valueOf(Arrays.asList(logLevels).get(logLevelIdx)));
logLevelTxt.setText(log.getLevel().name());
dialog.dismiss();
});
alertDialog.setNegativeButton("Cancel", (dialog, which) -> dialog.dismiss());
alertDialog.create();
alertDialog.show();
});

//Log messages
Button fatalLogBtn = findViewById(R.id.fatalLogBtn);
fatalLogBtn.setOnClickListener(v -> log.fatal("Current log level is: " + log.getLevel().name()));

Button errorLogBtn = findViewById(R.id.errorLogBtn);
errorLogBtn.setOnClickListener(v -> log.error("Current log level is: " + log.getLevel().name()));

Button warnLogBtn = findViewById(R.id.warnLogBtn);
warnLogBtn.setOnClickListener(v -> log.warn("Current log level is: " + log.getLevel().name()));

Button infoLogBtn = findViewById(R.id.infoLogBtn);
infoLogBtn.setOnClickListener(v -> log.info("Current log level is: " + log.getLevel().name()));

Button debugLogBtn = findViewById(R.id.debugLogBtn);
debugLogBtn.setOnClickListener(v -> log.debug("Current log level is: " + log.getLevel().name()));

Button traceLogBtn = findViewById(R.id.traceLogBtn);
traceLogBtn.setOnClickListener(v -> log.trace("Current log level is: " + log.getLevel().name()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
<aapt:attr name="android:fillColor">
<gradient
android:endX="85.84757"
android:endY="92.4963"
android:startX="42.9492"
android:startY="49.59793"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>
Loading
Loading