Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
日
日志记录sdk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
pengguangpu
日志记录sdk
Commits
7702eda0
Commit
7702eda0
authored
May 07, 2020
by
pengguangpu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
初步自测;
parent
45057d6e
Hide whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
675 additions
and
9 deletions
+675
-9
build.gradle
app/build.gradle
+1
-1
MainActivity.java
app/src/main/java/com/miya/miyaloggerdemo/MainActivity.java
+136
-0
activity_main.xml
app/src/main/res/layout/activity_main.xml
+93
-4
layout_list_log_file.xml
app/src/main/res/layout/layout_list_log_file.xml
+15
-0
build.gradle
build.gradle
+1
-1
build.gradle
miyalogger/build.gradle
+32
-2
AndroidManifest.xml
miyalogger/src/main/AndroidManifest.xml
+5
-1
MiyaLogger.java
miyalogger/src/main/java/com/miya/miyalogger/MiyaLogger.java
+392
-0
No files found.
app/build.gradle
View file @
7702eda0
...
...
@@ -20,9 +20,9 @@ android {
proguardFiles
getDefaultProguardFile
(
'proguard-android-optimize.txt'
),
'proguard-rules.pro'
}
}
}
dependencies
{
implementation
fileTree
(
dir:
'libs'
,
include:
[
'*.jar'
])
...
...
app/src/main/java/com/miya/miyaloggerdemo/MainActivity.java
View file @
7702eda0
...
...
@@ -3,6 +3,18 @@ package com.miya.miyaloggerdemo;
import
androidx.appcompat.app.AppCompatActivity
;
import
android.os.Bundle
;
import
android.text.TextUtils
;
import
android.view.View
;
import
android.widget.EditText
;
import
android.widget.ListView
;
import
android.widget.SimpleAdapter
;
import
com.miya.miyalogger.MiyaLogger
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
public
class
MainActivity
extends
AppCompatActivity
{
...
...
@@ -10,5 +22,129 @@ public class MainActivity extends AppCompatActivity {
protected
void
onCreate
(
Bundle
savedInstanceState
)
{
super
.
onCreate
(
savedInstanceState
);
setContentView
(
R
.
layout
.
activity_main
);
MiyaLogger
.
LOG_ENABLE
=
true
;
//刷新列表
findViewById
(
R
.
id
.
btnRefreshList
).
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
refreshListView
();
}
});
//删除all
findViewById
(
R
.
id
.
btnDeleteAllLog
).
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
MiyaLogger
.
deleteAllLog
();
}
});
//删除
findViewById
(
R
.
id
.
btnDeleteLog
).
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
String
daysStr
=
((
EditText
)
findViewById
(
R
.
id
.
etDays
)).
getText
().
toString
();
int
days
=
0
;
if
(
TextUtils
.
isEmpty
(
daysStr
))
{
days
=
Integer
.
parseInt
(
daysStr
);
}
MiyaLogger
.
deleteLog
(
days
);
}
});
//写日志
findViewById
(
R
.
id
.
btnWriteLog
).
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
MiyaLogger
.
writeLog
(
getApplication
().
getApplicationContext
(),
"caonima"
);
}
});
//log w
findViewById
(
R
.
id
.
btnW
).
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
MiyaLogger
.
w
(
"pupu"
,
"caonima"
);
}
});
//log e
findViewById
(
R
.
id
.
btnE
).
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
MiyaLogger
.
e
(
"pupu"
,
"caonima"
);
}
});
//log i
findViewById
(
R
.
id
.
btnI
).
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
MiyaLogger
.
i
(
"pupu"
,
"caonima"
);
}
});
//log v
findViewById
(
R
.
id
.
btnV
).
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
MiyaLogger
.
v
(
"pupu"
,
"caonima"
);
}
});
//log 4k
findViewById
(
R
.
id
.
btn4K
).
setOnClickListener
(
new
View
.
OnClickListener
()
{
@Override
public
void
onClick
(
View
v
)
{
MiyaLogger
.
i
(
"pupu"
,
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
""
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
"caonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonimacaonima"
+
""
);
}
});
//init listview
refreshListView
();
}
void
refreshListView
()
{
List
<
String
>
logFileList
=
MiyaLogger
.
getLogFileList
();
ListView
listView
=
findViewById
(
R
.
id
.
lvLogFile
);
List
<
Map
<
String
,
Object
>>
listData
=
new
ArrayList
<>();
if
(
logFileList
==
null
||
logFileList
.
size
()
==
0
)
{
}
else
{
for
(
String
temp
:
logFileList
)
{
MiyaLogger
.
i
(
"pupu"
,
"fileName==>"
+
temp
);
HashMap
<
String
,
Object
>
map
=
new
HashMap
<
String
,
Object
>();
map
.
put
(
"name"
,
temp
);
listData
.
add
(
map
);
}
}
listView
.
setAdapter
(
new
SimpleAdapter
(
this
,
listData
,
R
.
layout
.
layout_list_log_file
,
new
String
[]{
"name"
},
new
int
[]{
R
.
id
.
tvFileName
}));
}
}
app/src/main/res/layout/activity_main.xml
View file @
7702eda0
...
...
@@ -6,13 +6,102 @@
android:layout_height=
"match_parent"
tools:context=
".MainActivity"
>
<TextView
android:layout_width=
"wrap_content"
<Button
android:id=
"@+id/btnRefreshList"
android:layout_width=
"0dp"
android:layout_height=
"wrap_content"
android:text=
"Hello World!"
app:layout_constraintBottom_toBottomOf=
"parent"
android:text=
"刷新文件列表"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintRight_toRightOf=
"parent"
app:layout_constraintTop_toTopOf=
"parent"
/>
<Button
android:id=
"@+id/btnDeleteAllLog"
android:layout_width=
"0dp"
android:layout_height=
"wrap_content"
android:text=
"删除全部"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintRight_toRightOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@+id/btnRefreshList"
/>
<EditText
android:id=
"@+id/etDays"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:width=
"100dp"
android:hint=
"天数"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@+id/btnDeleteAllLog"
/>
<Button
android:id=
"@+id/btnDeleteLog"
android:layout_width=
"0dp"
android:layout_height=
"wrap_content"
android:text=
"删除指定天数日志"
app:layout_constraintLeft_toRightOf=
"@+id/etDays"
app:layout_constraintRight_toRightOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@+id/btnDeleteAllLog"
/>
<Button
android:id=
"@+id/btnWriteLog"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"写日志"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintRight_toRightOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@+id/btnDeleteLog"
/>
<Button
android:id=
"@+id/btnI"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"log i"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintRight_toRightOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@+id/btnWriteLog"
/>
<Button
android:id=
"@+id/btnW"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"log w"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintRight_toRightOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@+id/btnI"
/>
<Button
android:id=
"@+id/btnE"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"log e"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintRight_toRightOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@+id/btnW"
/>
<Button
android:id=
"@+id/btnV"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"log v"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintRight_toRightOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@+id/btnE"
/>
<Button
android:id=
"@+id/btn4K"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"log 4K"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintRight_toRightOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@+id/btnV"
/>
<ListView
android:id=
"@+id/lvLogFile"
android:layout_width=
"match_parent"
android:layout_height=
"0dp"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintTop_toBottomOf=
"@+id/btn4K"
app:layout_constraintVertical_weight=
"1"
></ListView>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
app/src/main/res/layout/layout_list_log_file.xml
0 → 100644
View file @
7702eda0
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"
xmlns:app=
"http://schemas.android.com/apk/res-auto"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
>
<TextView
android:id=
"@+id/tvFileName"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:textSize=
"18sp"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintTop_toTopOf=
"parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
build.gradle
View file @
7702eda0
...
...
@@ -20,7 +20,7 @@ allprojects {
repositories
{
google
()
jcenter
()
mavenLocal
()
}
}
...
...
miyalogger/build.gradle
View file @
7702eda0
apply
plugin:
'com.android.library'
apply
plugin:
'maven'
//获取生成的产品名
def
getSDKName
()
{
return
"miya-logger"
}
//发布版本
def
publishVersion
=
"1.0.0"
//发布版本号
def
publishVersionCode
=
1
android
{
compileSdkVersion
28
...
...
@@ -7,8 +17,8 @@ android {
defaultConfig
{
minSdkVersion
16
targetSdkVersion
28
versionCode
1
versionName
"1.0"
versionCode
publishVersionCode
versionName
publishVersion
testInstrumentationRunner
"androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles
'consumer-rules.pro'
...
...
@@ -21,6 +31,26 @@ android {
}
}
android
.
libraryVariants
.
all
{
variant
->
variant
.
outputs
.
all
{
outputFileName
=
getSDKName
()
+
"-${defaultConfig.versionName}"
+
'.aar'
}
}
}
//库上传任务
uploadArchives
{
repositories
.
mavenDeployer
{
//上传的url
repository
(
url:
"https://nexus.infra.miyatech.com/repository/shualianzhifu/"
)
{
authentication
(
userName:
"face-dev"
,
password:
"Face-dev@2019"
)
}
pom
.
version
=
publishVersion
pom
.
artifactId
=
"miya-logger-sdk"
pom
.
groupId
=
"com.miya.miyalogger"
}
}
dependencies
{
...
...
miyalogger/src/main/AndroidManifest.xml
View file @
7702eda0
<manifest
xmlns:android=
"http://schemas.android.com/apk/res/android"
package=
"com.miya.miyalogger"
/>
package=
"com.miya.miyalogger"
>
<uses-permission
android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
/>
<uses-permission
android:name=
"android.permission.READ_EXTERNAL_STORAGE"
/>
</manifest>
miyalogger/src/main/java/com/miya/miyalogger/MiyaLogger.java
0 → 100644
View file @
7702eda0
package
com
.
miya
.
miyalogger
;
import
android.content.Context
;
import
android.content.pm.PackageInfo
;
import
android.content.pm.PackageManager
;
import
android.os.AsyncTask
;
import
android.os.Environment
;
import
android.text.TextUtils
;
import
android.util.Log
;
import
androidx.annotation.NonNull
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.OutputStreamWriter
;
import
java.nio.charset.Charset
;
import
java.text.ParseException
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
import
java.util.concurrent.ScheduledExecutorService
;
public
class
MiyaLogger
{
private
static
File
logFiles
;
//日志文件夹
private
static
File
logFile
;
//单个日志文件
private
static
ScheduledExecutorService
sExecutor
;
public
static
final
String
CACHE_FILE_PATH
=
Environment
.
getExternalStorageDirectory
().
getPath
()
+
File
.
separator
+
"miyaterminal"
+
File
.
separator
+
"log"
+
File
.
separator
;
public
static
boolean
isProhibitWrite
=
false
;
//是否禁止写入文件
private
static
List
<
FileBean
>
allFileBeanList
;
//所有的文件列表
private
static
List
<
File
>
deleteFilePathList
;
//需要删除的文件列表
private
static
String
logSuffix
=
".txt"
;
private
static
String
logLink
=
"_"
;
public
static
final
String
DF_YYYYMMDD
=
"yyyyMMdd"
;
/**
* 将日期转换成字符串
*
* @param date
* @param format
* @return
*/
private
static
String
format
(
Date
date
,
String
format
)
{
if
(
date
==
null
)
{
throw
new
IllegalArgumentException
(
"Param date is null!"
);
}
if
(
TextUtils
.
isEmpty
(
format
))
{
throw
new
IllegalArgumentException
(
"Param format is blank!"
);
}
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
format
);
return
sdf
.
format
(
date
);
}
private
static
String
format8
(
Date
date
)
{
return
format
(
date
,
DF_YYYYMMDD
);
}
/**
* 将不同格式的 String 转换成 时间戳
*
* @param str
* @param formatStyle
* @return
*/
private
static
long
stringToLong
(
String
str
,
String
formatStyle
)
throws
ParseException
{
SimpleDateFormat
sdf
=
new
SimpleDateFormat
(
formatStyle
);
return
sdf
.
parse
(
str
).
getTime
();
//毫秒
}
/**
* 开启线程池,写日志,避免到处开启线程损耗性能
*
* @param input 写入内容
* @return
*/
public
static
boolean
writeLog
(
final
Context
c
,
final
String
input
)
{
if
(
sExecutor
==
null
)
{
sExecutor
=
Executors
.
newScheduledThreadPool
(
5
);
}
Future
<
Boolean
>
submit
=
sExecutor
.
submit
(
new
Callable
<
Boolean
>()
{
@Override
public
Boolean
call
()
{
try
{
logFiles
=
new
File
(
CACHE_FILE_PATH
);
if
(!
logFiles
.
exists
())
{
logFiles
.
mkdirs
();
}
String
logPath
=
CACHE_FILE_PATH
+
getVersionCode
(
c
)
+
"_"
+
format8
(
new
Date
())
+
".txt"
;
String
message
=
input
;
logFile
=
new
File
(
logPath
);
FileOutputStream
fos
=
new
FileOutputStream
(
logFile
,
true
);
OutputStreamWriter
writer
=
new
OutputStreamWriter
(
fos
,
Charset
.
forName
(
"utf-8"
));
writer
.
write
(
message
);
writer
.
write
(
"\n"
);
writer
.
flush
();
writer
.
close
();
return
true
;
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
false
;
}
}
});
try
{
return
submit
.
get
();
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
}
catch
(
ExecutionException
e
)
{
e
.
printStackTrace
();
}
return
false
;
}
public
static
String
getVersionCode
(
Context
context
)
{
PackageManager
packageManager
=
context
.
getPackageManager
();
PackageInfo
packageInfo
;
String
versionCode
=
""
;
try
{
packageInfo
=
packageManager
.
getPackageInfo
(
context
.
getPackageName
(),
0
);
versionCode
=
packageInfo
.
versionCode
+
""
;
}
catch
(
PackageManager
.
NameNotFoundException
e
)
{
e
.
printStackTrace
();
}
return
versionCode
;
}
/**
* 删除全部日志
*/
public
static
void
deleteAllLog
()
{
File
tempFile
=
new
File
(
CACHE_FILE_PATH
);
if
(
tempFile
==
null
||
tempFile
.
listFiles
()
==
null
||
tempFile
.
listFiles
().
length
==
0
)
{
return
;
}
new
DeleteLogTask
().
execute
(
tempFile
.
listFiles
());
}
/**
* 删除指定天数之前的日志,此处的天数不是精确到小时的,只精确到日
*
* @param days
*/
public
static
void
deleteLog
(
int
days
)
{
getFiles
();
if
(
deleteFilePathList
==
null
)
{
deleteFilePathList
=
new
ArrayList
<>();
}
else
{
deleteFilePathList
.
clear
();
}
if
(
allFileBeanList
!=
null
&&
allFileBeanList
.
size
()
>
0
)
{
for
(
int
i
=
0
;
i
<
allFileBeanList
.
size
();
i
++)
{
getDeleteFileList
(
days
,
allFileBeanList
.
get
(
i
));
}
}
if
(
deleteFilePathList
!=
null
&&
deleteFilePathList
.
size
()
>
0
)
{
File
[]
deleteFiles
=
deleteFilePathList
.
toArray
(
new
File
[
0
]);
new
DeleteLogTask
().
execute
(
deleteFiles
);
}
}
/**
* 执行符合条件的删除日志操作
*
* @param days
* @param fileBean
*/
private
static
void
getDeleteFileList
(
int
days
,
FileBean
fileBean
)
{
long
currentLongTimes
=
System
.
currentTimeMillis
();
if
(
fileBean
.
fileName
.
endsWith
(
logSuffix
))
{
if
(
fileBean
.
fileName
.
contains
(
logLink
))
{
try
{
String
fileNameWithOutSuffix
=
fileBean
.
fileName
.
substring
(
0
,
fileBean
.
fileName
.
indexOf
(
"."
));
String
fileNameDate
=
fileNameWithOutSuffix
.
substring
(
fileNameWithOutSuffix
.
lastIndexOf
(
logLink
)
+
1
);
long
fileNameLongTimes
=
stringToLong
(
fileNameDate
,
DF_YYYYMMDD
);
if
((
currentLongTimes
-
fileNameLongTimes
)
>=
days
*
24L
*
60L
*
60L
*
1000L
)
{
//符合要求,加入删除列表
deleteFilePathList
.
add
(
new
File
(
fileBean
.
filePath
));
}
}
catch
(
ParseException
e
)
{
e
.
printStackTrace
();
}
}
}
}
/**
* 查询所有日志
*/
public
static
List
<
String
>
getLogFileList
()
{
File
tempFile
=
new
File
(
CACHE_FILE_PATH
);
if
(
tempFile
!=
null
)
{
List
<
String
>
result
=
new
ArrayList
<>();
if
(
tempFile
.
listFiles
()
==
null
||
tempFile
.
listFiles
().
length
==
0
)
{
return
null
;
}
for
(
File
file
:
tempFile
.
listFiles
())
{
result
.
add
(
file
.
getName
());
}
return
result
;
}
return
null
;
}
/**
* 获得日志文件夹下的文件名和文件路径
*/
private
static
void
getFiles
()
{
if
(
allFileBeanList
==
null
)
{
allFileBeanList
=
new
ArrayList
<>();
}
else
{
allFileBeanList
.
clear
();
}
File
tempFile
=
new
File
(
CACHE_FILE_PATH
);
if
(
tempFile
==
null
)
{
return
;
}
File
[]
files
=
tempFile
.
listFiles
();
if
(
files
!=
null
)
{
for
(
int
i
=
0
;
i
<
files
.
length
;
i
++)
{
FileBean
fileBean
=
new
FileBean
();
fileBean
.
fileName
=
files
[
i
].
getName
();
fileBean
.
filePath
=
files
[
i
].
getAbsolutePath
();
allFileBeanList
.
add
(
fileBean
);
}
}
}
private
static
class
DeleteLogTask
extends
AsyncTask
<
File
,
Void
,
Boolean
>
{
@Override
protected
Boolean
doInBackground
(
File
...
files
)
{
for
(
File
file
:
files
)
{
doDeleteFiles
(
file
);
}
return
true
;
}
@Override
protected
void
onPostExecute
(
Boolean
aBoolean
)
{
super
.
onPostExecute
(
aBoolean
);
if
(
aBoolean
)
{
Log
.
i
(
"LogFileUtils"
,
"日志删除成功!"
);
}
}
}
private
static
void
doDeleteFiles
(
File
file
)
{
if
(
file
==
null
)
{
return
;
}
//flie:要删除的文件夹的所在位置
if
(
file
.
isDirectory
())
{
File
[]
files
=
file
.
listFiles
();
for
(
int
i
=
0
;
i
<
files
.
length
;
i
++)
{
File
f
=
files
[
i
];
doDeleteFiles
(
f
);
}
file
.
delete
();
//如要保留文件夹,只删除文件,请注释这行
}
else
if
(
file
.
exists
())
{
file
.
delete
();
}
}
private
static
class
FileBean
{
String
fileName
;
String
filePath
;
}
//设为false关闭日志
public
static
boolean
LOG_ENABLE
=
true
;
public
static
void
i
(
String
tag
,
@NonNull
String
msg
)
{
if
(
LOG_ENABLE
)
{
//当文本量超过4K时,循环打印
msg
=
msg
.
trim
();
int
index
=
0
;
int
maxLength
=
4000
;
String
sub
;
while
(
index
<
msg
.
length
())
{
// java的字符不允许指定超过总的长度end
if
(
msg
.
length
()
<=
index
+
maxLength
)
{
sub
=
msg
.
substring
(
index
);
}
else
{
sub
=
msg
.
substring
(
index
,
maxLength
);
}
index
+=
maxLength
;
Log
.
i
(
tag
,
sub
.
trim
());
}
}
}
public
static
void
v
(
String
tag
,
@NonNull
String
msg
)
{
if
(
LOG_ENABLE
)
{
//当文本量超过4K时,循环打印
msg
=
msg
.
trim
();
int
index
=
0
;
int
maxLength
=
4000
;
String
sub
;
while
(
index
<
msg
.
length
())
{
// java的字符不允许指定超过总的长度end
if
(
msg
.
length
()
<=
index
+
maxLength
)
{
sub
=
msg
.
substring
(
index
);
}
else
{
sub
=
msg
.
substring
(
index
,
maxLength
);
}
index
+=
maxLength
;
Log
.
v
(
tag
,
sub
.
trim
());
}
}
}
public
static
void
d
(
String
tag
,
@NonNull
String
msg
)
{
if
(
LOG_ENABLE
)
{
//当文本量超过4K时,循环打印
msg
=
msg
.
trim
();
int
index
=
0
;
int
maxLength
=
4000
;
String
sub
;
while
(
index
<
msg
.
length
())
{
// java的字符不允许指定超过总的长度end
if
(
msg
.
length
()
<=
index
+
maxLength
)
{
sub
=
msg
.
substring
(
index
);
}
else
{
sub
=
msg
.
substring
(
index
,
maxLength
);
}
index
+=
maxLength
;
Log
.
d
(
tag
,
sub
.
trim
());
}
}
}
public
static
void
w
(
String
tag
,
@NonNull
String
msg
)
{
if
(
LOG_ENABLE
)
{
//当文本量超过4K时,循环打印
msg
=
msg
.
trim
();
int
index
=
0
;
int
maxLength
=
4000
;
String
sub
;
while
(
index
<
msg
.
length
())
{
// java的字符不允许指定超过总的长度end
if
(
msg
.
length
()
<=
index
+
maxLength
)
{
sub
=
msg
.
substring
(
index
);
}
else
{
sub
=
msg
.
substring
(
index
,
maxLength
);
}
index
+=
maxLength
;
Log
.
w
(
tag
,
sub
.
trim
());
}
}
}
public
static
void
e
(
String
tag
,
@NonNull
String
msg
)
{
if
(
LOG_ENABLE
)
{
//当文本量超过4K时,循环打印
msg
=
msg
.
trim
();
int
index
=
0
;
int
maxLength
=
4000
;
String
sub
;
while
(
index
<
msg
.
length
())
{
// java的字符不允许指定超过总的长度end
if
(
msg
.
length
()
<=
index
+
maxLength
)
{
sub
=
msg
.
substring
(
index
);
}
else
{
sub
=
msg
.
substring
(
index
,
maxLength
);
}
index
+=
maxLength
;
Log
.
e
(
tag
,
sub
.
trim
());
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment