From 34e0c171ddfb96d5f8b560f919f9fb8dba1b2411 Mon Sep 17 00:00:00 2001 From: Nick White Date: Mon, 7 Oct 2024 17:40:38 +0100 Subject: [PATCH] Adding log4j-samples-android project --- log4j-samples-android/.gitignore | 15 ++ log4j-samples-android/app/.gitignore | 1 + log4j-samples-android/app/build.gradle | 48 +++++ log4j-samples-android/app/proguard-rules.pro | 21 ++ .../log4japi/ExampleInstrumentedTest.java | 26 +++ .../app/src/main/AndroidManifest.xml | 27 +++ .../app/src/main/assets/log4j2.properties | 21 ++ .../com/example/log4japi/MainActivity.java | 96 +++++++++ .../drawable-v24/ic_launcher_foreground.xml | 30 +++ .../res/drawable/ic_launcher_background.xml | 170 ++++++++++++++++ .../app/src/main/res/layout/activity_main.xml | 114 +++++++++++ .../res/mipmap-anydpi-v26/ic_launcher.xml | 5 + .../mipmap-anydpi-v26/ic_launcher_round.xml | 5 + .../src/main/res/mipmap-hdpi/ic_launcher.webp | Bin 0 -> 1404 bytes .../res/mipmap-hdpi/ic_launcher_round.webp | Bin 0 -> 2898 bytes .../src/main/res/mipmap-mdpi/ic_launcher.webp | Bin 0 -> 982 bytes .../res/mipmap-mdpi/ic_launcher_round.webp | Bin 0 -> 1772 bytes .../main/res/mipmap-xhdpi/ic_launcher.webp | Bin 0 -> 1900 bytes .../res/mipmap-xhdpi/ic_launcher_round.webp | Bin 0 -> 3918 bytes .../main/res/mipmap-xxhdpi/ic_launcher.webp | Bin 0 -> 2884 bytes .../res/mipmap-xxhdpi/ic_launcher_round.webp | Bin 0 -> 5914 bytes .../main/res/mipmap-xxxhdpi/ic_launcher.webp | Bin 0 -> 3844 bytes .../res/mipmap-xxxhdpi/ic_launcher_round.webp | Bin 0 -> 7778 bytes .../app/src/main/res/values-night/themes.xml | 16 ++ .../app/src/main/res/values/colors.xml | 10 + .../app/src/main/res/values/strings.xml | 3 + .../app/src/main/res/values/themes.xml | 16 ++ .../app/src/main/res/xml/backup_rules.xml | 13 ++ .../main/res/xml/data_extraction_rules.xml | 19 ++ .../com/example/log4japi/ExampleUnitTest.java | 17 ++ log4j-samples-android/build.gradle | 9 + log4j-samples-android/gradle.properties | 21 ++ .../gradle/wrapper/gradle-wrapper.jar | Bin 0 -> 59203 bytes .../gradle/wrapper/gradle-wrapper.properties | 6 + log4j-samples-android/gradlew | 185 ++++++++++++++++++ log4j-samples-android/gradlew.bat | 89 +++++++++ log4j-samples-android/settings.gradle | 16 ++ 37 files changed, 999 insertions(+) create mode 100644 log4j-samples-android/.gitignore create mode 100644 log4j-samples-android/app/.gitignore create mode 100644 log4j-samples-android/app/build.gradle create mode 100644 log4j-samples-android/app/proguard-rules.pro create mode 100644 log4j-samples-android/app/src/androidTest/java/com/example/log4japi/ExampleInstrumentedTest.java create mode 100644 log4j-samples-android/app/src/main/AndroidManifest.xml create mode 100644 log4j-samples-android/app/src/main/assets/log4j2.properties create mode 100644 log4j-samples-android/app/src/main/java/com/example/log4japi/MainActivity.java create mode 100644 log4j-samples-android/app/src/main/res/drawable-v24/ic_launcher_foreground.xml create mode 100644 log4j-samples-android/app/src/main/res/drawable/ic_launcher_background.xml create mode 100644 log4j-samples-android/app/src/main/res/layout/activity_main.xml create mode 100644 log4j-samples-android/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml create mode 100644 log4j-samples-android/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml create mode 100644 log4j-samples-android/app/src/main/res/mipmap-hdpi/ic_launcher.webp create mode 100644 log4j-samples-android/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp create mode 100644 log4j-samples-android/app/src/main/res/mipmap-mdpi/ic_launcher.webp create mode 100644 log4j-samples-android/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp create mode 100644 log4j-samples-android/app/src/main/res/mipmap-xhdpi/ic_launcher.webp create mode 100644 log4j-samples-android/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp create mode 100644 log4j-samples-android/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp create mode 100644 log4j-samples-android/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp create mode 100644 log4j-samples-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp create mode 100644 log4j-samples-android/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp create mode 100644 log4j-samples-android/app/src/main/res/values-night/themes.xml create mode 100644 log4j-samples-android/app/src/main/res/values/colors.xml create mode 100644 log4j-samples-android/app/src/main/res/values/strings.xml create mode 100644 log4j-samples-android/app/src/main/res/values/themes.xml create mode 100644 log4j-samples-android/app/src/main/res/xml/backup_rules.xml create mode 100644 log4j-samples-android/app/src/main/res/xml/data_extraction_rules.xml create mode 100644 log4j-samples-android/app/src/test/java/com/example/log4japi/ExampleUnitTest.java create mode 100644 log4j-samples-android/build.gradle create mode 100644 log4j-samples-android/gradle.properties create mode 100644 log4j-samples-android/gradle/wrapper/gradle-wrapper.jar create mode 100644 log4j-samples-android/gradle/wrapper/gradle-wrapper.properties create mode 100644 log4j-samples-android/gradlew create mode 100644 log4j-samples-android/gradlew.bat create mode 100644 log4j-samples-android/settings.gradle diff --git a/log4j-samples-android/.gitignore b/log4j-samples-android/.gitignore new file mode 100644 index 0000000..aa724b7 --- /dev/null +++ b/log4j-samples-android/.gitignore @@ -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 diff --git a/log4j-samples-android/app/.gitignore b/log4j-samples-android/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/log4j-samples-android/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/log4j-samples-android/app/build.gradle b/log4j-samples-android/app/build.gradle new file mode 100644 index 0000000..50b9b5e --- /dev/null +++ b/log4j-samples-android/app/build.gradle @@ -0,0 +1,48 @@ +plugins { + id 'com.android.application' +} + +android { + compileSdk 34 + + defaultConfig { + applicationId "com.example.log4japi" + 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' +} \ No newline at end of file diff --git a/log4j-samples-android/app/proguard-rules.pro b/log4j-samples-android/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/log4j-samples-android/app/proguard-rules.pro @@ -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 \ No newline at end of file diff --git a/log4j-samples-android/app/src/androidTest/java/com/example/log4japi/ExampleInstrumentedTest.java b/log4j-samples-android/app/src/androidTest/java/com/example/log4japi/ExampleInstrumentedTest.java new file mode 100644 index 0000000..a7712f6 --- /dev/null +++ b/log4j-samples-android/app/src/androidTest/java/com/example/log4japi/ExampleInstrumentedTest.java @@ -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 Testing documentation + */ +@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()); + } +} \ No newline at end of file diff --git a/log4j-samples-android/app/src/main/AndroidManifest.xml b/log4j-samples-android/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..ba30a62 --- /dev/null +++ b/log4j-samples-android/app/src/main/AndroidManifest.xml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/log4j-samples-android/app/src/main/assets/log4j2.properties b/log4j-samples-android/app/src/main/assets/log4j2.properties new file mode 100644 index 0000000..508cf7e --- /dev/null +++ b/log4j-samples-android/app/src/main/assets/log4j2.properties @@ -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 \ No newline at end of file diff --git a/log4j-samples-android/app/src/main/java/com/example/log4japi/MainActivity.java b/log4j-samples-android/app/src/main/java/com/example/log4japi/MainActivity.java new file mode 100644 index 0000000..1749ed7 --- /dev/null +++ b/log4j-samples-android/app/src/main/java/com/example/log4japi/MainActivity.java @@ -0,0 +1,96 @@ +package com.example.log4japi; + +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.LoggerContext; +import org.apache.logging.log4j.core.config.ConfigurationFactory; +import org.apache.logging.log4j.core.config.ConfigurationSource; +import org.apache.logging.log4j.core.config.Configurator; +import org.apache.logging.log4j.core.config.LoggerConfig; + +import java.io.IOException; +import java.io.InputStream; +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())); + } +} \ No newline at end of file diff --git a/log4j-samples-android/app/src/main/res/drawable-v24/ic_launcher_foreground.xml b/log4j-samples-android/app/src/main/res/drawable-v24/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/log4j-samples-android/app/src/main/res/drawable-v24/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/log4j-samples-android/app/src/main/res/drawable/ic_launcher_background.xml b/log4j-samples-android/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/log4j-samples-android/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/log4j-samples-android/app/src/main/res/layout/activity_main.xml b/log4j-samples-android/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..2fc7b76 --- /dev/null +++ b/log4j-samples-android/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,114 @@ + + + + + + + + + +