diff --git a/log4j-samples-android/.gitignore b/log4j-samples-android/.gitignore new file mode 100644 index 00000000..aa724b77 --- /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 00000000..42afabfd --- /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 00000000..e209137d --- /dev/null +++ b/log4j-samples-android/app/build.gradle @@ -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' +} \ 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 00000000..481bb434 --- /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 00000000..a7712f65 --- /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 00000000..228a0ecf --- /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 00000000..508cf7ec --- /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/org/apache/logging/log4j/samples/android/MainActivity.java b/log4j-samples-android/app/src/main/java/org/apache/logging/log4j/samples/android/MainActivity.java new file mode 100644 index 00000000..7797f6a1 --- /dev/null +++ b/log4j-samples-android/app/src/main/java/org/apache/logging/log4j/samples/android/MainActivity.java @@ -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())); + } +} \ 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 00000000..2b068d11 --- /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 00000000..07d5da9c --- /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 00000000..2fc7b76e --- /dev/null +++ b/log4j-samples-android/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,114 @@ + + + + + + + + + +