Merge pull request 'ロガーを追加' (#46) from feature/add_logger into main

Reviewed-on: #46
This commit is contained in:
Fujimatsu 2024-06-07 00:52:02 +00:00
commit a20240e88f
21 changed files with 386 additions and 0 deletions

View File

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
@ -14,6 +15,7 @@
<option value="$PROJECT_DIR$/feature/debug" />
<option value="$PROJECT_DIR$/feature/parent" />
<option value="$PROJECT_DIR$/feature/setting" />
<option value="$PROJECT_DIR$/utils" />
</set>
</option>
<option name="resolveExternalAnnotations" value="false" />

View File

@ -14,6 +14,9 @@ import androidx.navigation.ui.NavigationUI;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import dagger.hilt.android.AndroidEntryPoint;
@AndroidEntryPoint
public class MainActivity extends AppCompatActivity {
@Override

View File

@ -0,0 +1,4 @@
<resources>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>

View File

@ -39,4 +39,7 @@ dependencies {
// Hilt (DI)
implementation libs.com.google.dagger.hilt.android
annotationProcessor libs.com.google.dagger.hilt.compiler
// App Module
implementation project(':utils')
}

View File

@ -8,13 +8,22 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import javax.inject.Inject;
import dagger.hilt.android.AndroidEntryPoint;
import one.nem.kidshift.utils.KSLogger;
/**
* A simple {@link Fragment} subclass.
* Use the {@link DebugMainFragment#newInstance} factory method to
* create an instance of this fragment.
*/
@AndroidEntryPoint
public class DebugMainFragment extends Fragment {
@Inject
KSLogger ksLogger;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
@ -61,4 +70,13 @@ public class DebugMainFragment extends Fragment {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_debug_main, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.insertDebugLogButton).setOnClickListener(v -> {
ksLogger.debug("Log inserted from DebugMainFragment!");
});
}
}

View File

@ -8,6 +8,7 @@
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="🔧DEBUG"
@ -17,4 +18,14 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/insertDebugLogButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Insert debug log!"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>

View File

@ -0,0 +1,4 @@
<resources>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>

View File

@ -0,0 +1,4 @@
<resources>
<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>

View File

@ -25,3 +25,4 @@ include ':feature:parent'
include ':feature:child'
include ':feature:setting'
include ':feature:debug'
include ':utils'

1
utils/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/build

40
utils/build.gradle Normal file
View File

@ -0,0 +1,40 @@
plugins {
alias(libs.plugins.androidLibrary)
id 'com.google.dagger.hilt.android'
}
android {
namespace 'one.nem.kidshift.utils'
compileSdk 34
defaultConfig {
minSdk 28
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation libs.appcompat
implementation libs.material
testImplementation libs.junit
androidTestImplementation libs.ext.junit
androidTestImplementation libs.espresso.core
// Hilt (DI)
implementation libs.com.google.dagger.hilt.android
annotationProcessor libs.com.google.dagger.hilt.compiler
}

0
utils/consumer-rules.pro Normal file
View File

21
utils/proguard-rules.pro vendored Normal file
View File

@ -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

View File

@ -0,0 +1,26 @@
package one.nem.kidshift.utils;
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("one.nem.kidshift.utils.test", appContext.getPackageName());
}
}

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>

View File

@ -0,0 +1,14 @@
package one.nem.kidshift.utils;
public interface KSLogger {
KSLogger getChildLogger(String tag);
KSLogger get(String tag);
KSLogger setTag(String tag);
KSLogger addTag(String tag);
void info(String message);
void warn(String message);
void error(String message);
void debug(String message);
void trace(String message);
void fatal(String message);
}

View File

@ -0,0 +1,10 @@
package one.nem.kidshift.utils.enums;
public enum LogLevelEnum {
INFO,
WARN,
ERROR,
DEBUG,
TRACE,
FATAL
}

View File

@ -0,0 +1,124 @@
package one.nem.kidshift.utils.impl;
import static one.nem.kidshift.utils.enums.LogLevelEnum.INFO;
import android.util.Log;
import java.util.ArrayList;
import javax.inject.Inject;
import one.nem.kidshift.utils.KSLogger;
import one.nem.kidshift.utils.enums.LogLevelEnum;
import one.nem.kidshift.utils.models.LogModel;
public class KSLoggerImpl implements KSLogger {
private ArrayList<LogModel> logs = new ArrayList<LogModel>();
private ArrayList<String> tags = new ArrayList<String>();
@Inject
public KSLoggerImpl() {
tags.add("UNTAGGED");
}
public KSLoggerImpl(String tag) {
tags.add(tag);
}
@Override
public KSLogger getChildLogger(String tag) {
tags.add(tag);
return this;
}
@Override
public KSLogger get(String tag) {
return new KSLoggerImpl(tag);
}
@Override
public KSLogger setTag(String tag) {
tags.clear();
tags.add(tag);
return this;
}
@Override
public KSLogger addTag(String tag) {
tags.add(tag);
return this;
}
@Override
public void info(String message) {
log(new LogModel(LogLevelEnum.INFO, new String[]{}, message));
}
@Override
public void warn(String message) {
log(new LogModel(LogLevelEnum.WARN, new String[]{}, message));
}
@Override
public void error(String message) {
log(new LogModel(LogLevelEnum.ERROR, new String[]{}, message));
}
@Override
public void debug(String message) {
log(new LogModel(LogLevelEnum.DEBUG, new String[]{}, message));
}
@Override
public void trace(String message) {
log(new LogModel(LogLevelEnum.TRACE, new String[]{}, message));
}
@Override
public void fatal(String message) {
log(new LogModel(LogLevelEnum.FATAL, new String[]{}, message));
}
private void log(LogModel log) {
addLog(log);
outputLog(log);
}
private void addLog(LogModel log) {
logs.add(log);
}
private void outputLog(LogModel log) {
// ,区切りで出力
String tags = log.getTags().length > 0 ? String.join(",", log.getTags()) : "UNTAGGED";
LogLevelEnum level = log.getLogLevel();
String message = log.getMessage();
switch (level) {
case INFO:
Log.i(tags, message);
break;
case WARN:
Log.w(tags, message);
break;
case ERROR:
Log.e(tags, message);
break;
case DEBUG:
Log.d(tags, message);
break;
case TRACE:
Log.v(tags, message);
break;
case FATAL:
Log.wtf(tags, message);
break;
default:
Log.i(tags, message);
break;
}
}
}

View File

@ -0,0 +1,63 @@
package one.nem.kidshift.utils.models;
import java.util.Date;
import one.nem.kidshift.utils.enums.LogLevelEnum;
public class LogModel {
private Date eventTime;
private LogLevelEnum logLevel;
private String[] tags;
private String message;
public LogModel(Date eventTime, LogLevelEnum logLevel, String[] tags, String message) {
this.eventTime = eventTime;
this.logLevel = logLevel;
this.tags = tags;
this.message = message;
}
public LogModel(LogLevelEnum logLevel, String[] tags, String message) {
this.eventTime = new Date();
this.logLevel = logLevel;
this.tags = tags;
this.message = message;
}
public LogModel() {
}
// Getters and Setters
public Date getEventTime() {
return eventTime;
}
public void setEventTime(Date eventTime) {
this.eventTime = eventTime;
}
public LogLevelEnum getLogLevel() {
return logLevel;
}
public void setLogLevel(LogLevelEnum logLevel) {
this.logLevel = logLevel;
}
public String[] getTags() {
return tags;
}
public void setTags(String[] tags) {
this.tags = tags;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}

View File

@ -0,0 +1,16 @@
package one.nem.kidshift.utils.modules;
import dagger.Binds;
import dagger.Module;
import dagger.hilt.InstallIn;
import dagger.hilt.components.SingletonComponent;
import one.nem.kidshift.utils.KSLogger;
import one.nem.kidshift.utils.impl.KSLoggerImpl;
@Module
@InstallIn(SingletonComponent.class)
abstract public class KSLoggerModule {
@Binds
public abstract KSLogger bindKSLogger(KSLoggerImpl ksLoggerImpl);
}

View File

@ -0,0 +1,17 @@
package one.nem.kidshift.utils;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() {
assertEquals(4, 2 + 2);
}
}