【背景】html
折騰:java
【記錄】給Android中添加log日誌輸出到文件android
期間,已經試了:express
【記錄】嘗試用android中microlog4android實現log輸出到文件的功能apache
可是很差用。api
而後就是參考:app
http://stackoverflow.com/questions/2116260/logging-to-a-file-on-androidless
去看看:ide
http://code.google.com/p/android-logging-log4j/測試
【[折騰過程】
1.去:
https://code.google.com/p/android-logging-log4j/downloads/list
下載:
android-logging-log4j-1.0.3.jar
而後導入庫,寫示例代碼。
結果導入的時候,就說不支持:
1
|
import
org.apache.log4j.Level;
|
2.而後找到:
http://logging.apache.org/log4j/2.x/
而後去下載:
apache-log4j-2.0-beta9-bin.zip
發現有4M多,好大。
3.而後換用:
http://logging.apache.org/log4j/1.2/download.html
中的:
後來發現:
裏面有:
36KB的log4j-1.2-api-2.0-beta9.jar
log4j-1.2.17\apache-log4j-1.2.17中有:479KB的log4j-1.2.17.jar
因此仍是去用:
apache-log4j-2.0-beta9-bin\log4j-1.2-api-2.0-beta9.jar
加到項目中。
4.後來發現:
1
|
final
Logger gLogger = Logger.getLogger(logConfigurator.getClass());
|
會出錯,貌似須要導入:
apache-log4j-2.0-beta9-bin
下面的:
log4j-api-2.0-beta9.jar
(和? 或?)
log4j-core-2.0-beta9.jar
而後發現導入:
log4j-core-2.0-beta9.jar
就能夠了。
5.最後去試試代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import
org.apache.log4j.Level;
import
org.apache.log4j.Logger;
import
de.mindpipe.android.logging.log4j.LogConfigurator;
public
class
BaseActivity
extends
Activity {
final
LogConfigurator logConfigurator =
new
LogConfigurator();
final
Logger gLogger = Logger.getLogger(logConfigurator.getClass());
//public Logger createLogFile()
public
void
configLog()
{
logConfigurator.setFileName(Environment.getExternalStorageDirectory() +
"crifanli_log4j.log"
);
logConfigurator.setRootLevel(Level.DEBUG);
// Set log level of a specific logger
logConfigurator.setLevel(
"org.apache"
, Level.ERROR);
logConfigurator.configure();
}
@Override
protected
void
onCreate(Bundle savedInstanceState) {
configLog();
gLogger.debug(
"test android log to file using log4j"
);
|
效果如何,
貌似發現會掛掉。
6.而後再去把:
log4j-api-2.0-beta9.jar
也加進來,看看是否能夠。
貌似仍是不行。
而後發現是代碼寫的有問題:
還沒初始化,就去getLogger了。
因此改成:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
import
org.apache.log4j.Level;
import
org.apache.log4j.Logger;
import
de.mindpipe.android.logging.log4j.LogConfigurator;
public
class
BaseActivity
extends
Activity {
private
LogConfigurator logConfigurator;
private
Logger gLogger;
public
void
configLog()
{
logConfigurator =
new
LogConfigurator();
logConfigurator.setFileName(Environment.getExternalStorageDirectory() +
"crifanli_log4j.log"
);
logConfigurator.setRootLevel(Level.DEBUG);
// Set log level of a specific logger
logConfigurator.setLevel(
"org.apache"
, Level.ERROR);
logConfigurator.configure();
gLogger = Logger.getLogger(logConfigurator.getClass());
}
@Override
protected
void
onCreate(Bundle savedInstanceState) {
configLog();
gLogger.debug(
"test android log to file using log4j"
);
|
結果是:
在:
1
|
logConfigurator.setLevel(
"org.apache"
, Level.ERROR);
|
會掛掉:
7.而後能夠正常生成log文件:
對應的log的內容,仍是不錯的:
至此:
終於能夠在android中,正常使用這個log4j去實現log輸出內容到sd中的文件了。
8.目前,正常工做的配置和代碼是:
(1)android項目中,導入:
- android-logging-log4j-1.0.3.jar
- log4j-1.2.17.jar
- 注意:我此處用apache-log4j-2.0-beta9-bin中的log4j-1.2-api-2.0-beta9.jar + log4j-core-2.0-beta9.jar 會出錯:
- 這句:logConfigurator.setLevel("org.apache", Level.ERROR);,會掛掉的。
- 注意:我此處用apache-log4j-2.0-beta9-bin中的log4j-1.2-api-2.0-beta9.jar + log4j-core-2.0-beta9.jar 會出錯:
如圖:
(2)代碼這麼寫:
(a)BaseActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
import
org.apache.log4j.Level;
import
org.apache.log4j.Logger;
public
class
BaseActivity
extends
Activity {
private
Logger gLogger;
public
void
configLog()
{
ConfigureLog4J.configure();
gLogger = Logger.getLogger(ExampleLog4J.
class
);
}
@Override
protected
void
onCreate(Bundle savedInstanceState) {
configLog();
gLogger.debug(
"test android log to file using log4j"
);
|
(b)ExampleLog4J.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/*
Copyright 2011 Rolf Kulemann
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.mm.mobilehandheld.common;
import org.apache.log4j.Logger;
/**
* Demonstrates using log4j directly.
* @author Rolf Kulemann
*/
public
class
ExampleLog4J {
private
final
Logger log = Logger.getLogger(ExampleLog4J.
class
);
public
void
myMethod() {
log.info(
"This message should be seen in log file and logcat"
);
}
}
|
(c)ConfigureLog4J.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
/*
Copyright 2011 Rolf Kulemann
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.mm.mobilehandheld.common;
import java.io.File;
import org.apache.log4j.Level;
import android.os.Environment;
import de.mindpipe.android.logging.log4j.LogConfigurator;
/**
* Example how to to configure Log4J in Android.
* Call {@link #configure()}} from your application's activity.
* @author Rolf Kulemann
*/
public
class
ConfigureLog4J {
public
static
void
configure() {
final
LogConfigurator logConfigurator =
new
LogConfigurator();
logConfigurator.setFileName(Environment.getExternalStorageDirectory() + File.separator +
"myapp.log"
);
// Set the root log level
logConfigurator.setRootLevel(Level.DEBUG);
// Set log level of a specific logger
logConfigurator.setLevel(
"org.apache"
, Level.ERROR);
logConfigurator.configure();
}
}
|
便可。
不過,很明顯,爲了簡單的log到文件,弄了這麼多類,這麼多文件,很不爽。
9.因此嘗試繼續看看,可否優化代碼:
儘可能減小文件,把相關代碼弄在一個文件裏面。
而後用以下代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
import
java.io.File;
import
android.os.Environment;
import
org.apache.log4j.Level;
import
org.apache.log4j.Logger;
public
class
BaseActivity
extends
Activity {
private
Logger gLogger;
public
void
configLog()
{
final
LogConfigurator logConfigurator =
new
LogConfigurator();
logConfigurator.setFileName(Environment.getExternalStorageDirectory() + File.separator +
"crifanli_log4j.log"
);
// Set the root log level
logConfigurator.setRootLevel(Level.DEBUG);
// Set log level of a specific logger
logConfigurator.setLevel(
"org.apache"
, Level.ERROR);
logConfigurator.configure();
//gLogger = Logger.getLogger(this.getClass());
gLogger = Logger.getLogger(
"CrifanLiLog4jTest"
);
}
@Override
protected
void
onCreate(Bundle savedInstanceState) {
configLog();
gLogger.debug(
"test android log to file in sd card using log4j"
);
|
測試效果爲:
log的內容爲:
【總結】
至此,終於基本實現須要的。
在android中,實現輸出log內容到sd卡中的文件裏面,作法是:
仍是相對來講,log4j,算是好用。
1.下載android的log4j的庫(的封裝)
去:http://code.google.com/p/android-logging-log4j/
下載對應的android-logging-log4j-1.0.3.jar,加到項目中。
2.再去下載所依賴的apache的log4j庫
去:http://logging.apache.org/log4j/1.2/download.html
下載1.2系列版本的:log4j-1.2.17.zip
解壓獲得log4j-1.2.17.jar加到項目中。
3.寫測試代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
import
de.mindpipe.android.logging.log4j.LogConfigurator;
import
java.io.File;
import
android.os.Environment;
import
org.apache.log4j.Level;
import
org.apache.log4j.Logger;
public
class
BaseActivity
extends
Activity {
private
Logger gLogger;
public
void
configLog()
{
final
LogConfigurator logConfigurator =
new
LogConfigurator();
logConfigurator.setFileName(Environment.getExternalStorageDirectory() + File.separator +
"crifanli_log4j.log"
);
// Set the root log level
logConfigurator.setRootLevel(Level.DEBUG);
// Set log level of a specific logger
logConfigurator.setLevel(
"org.apache"
, Level.ERROR);
logConfigurator.configure();
//gLogger = Logger.getLogger(this.getClass());
gLogger = Logger.getLogger(
"CrifanLiLog4jTest"
);
}
@Override
protected
void
onCreate(Bundle savedInstanceState) {
configLog();
gLogger.debug(
"test android log to file in sd card using log4j"
);
|
便可實現:
(1)能夠在/mnt/sdcard中生成對應的crifanli_log4j.log文件
(2)log輸出的內容中,是DEBUG,且對應的是本身的字符串標識符CrifanLiLog4jTest
轉自:http://www.crifan.com/android_try_use_android_logging_log4j_to_output_log_to_sd_card_file/