【背景】
折騰:
期間,已經試了:
【記錄】嘗試用android中microlog4android實現log輸出到文件的功能
但是不好用。
然后就是參考:
http://stackoverflow.com/questions/2116260/logging-to-a-file-on-android
去看看:
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);,會掛掉的。
如圖:
(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
2 Thoughts on “【記錄】嘗試用android-logging-log4j去實現log輸出內容到sd卡中的文件的功能”