Comments (2)
Instead of trying to prevent the exception (a symptom), I think you should be wondering how to get the SD card's path into a variable for use in logback configuration files. This assumes you prefer maximum portability by not using an absolute path to the SD card (which is smart :).
The init()
function you're using is flawed and actually unnecessary. It looks like you were trying to restart the auto-config after setting an environment variable to store the SD path (so that you can use that variable in a file path for the RollingFileAppender
). And then you were printing the JoranException
once in a catch
block, again by StatusPrinter.printInCaseOfErrorsOrWarnings
, and yet again from the debug='true'
setting from your logback config. So, you'd see the same error printed 3 times in logcat (probably not what you wanted). Finally, the init()
directly uses classes from logback, thus adding an unnecessary dependency and defeating the point of the SLF4J facade.
Luckily, you can accomplish your goal without all this init()
stuff you have going on. Android exposes the SD card path in an environment variable, named EXTERNAL_STORAGE
. (I'm not sure which Android versions expose this, but I just verified Jelly Bean 4.1.1.) Logback configuration can read environment variables and can define local properties. Armed with these tools, you can use the following config without any initialization code and without any extra dependencies beyond SLF4J (note the single <property>
element I added to your original config):
<configuration>
<appender
name="LOGCAT"
class="ch.qos.logback.classic.android.LogcatAppender" >
<tagEncoder>
<pattern>%logger{0}</pattern>
</tagEncoder>
<encoder>
<pattern>%d{HH:mm:ss} - %msg %n</pattern>
</encoder>
</appender>
<!--
########################################################
Use Android's built-in environment variable for the
path to the SD card: EXTERNAL_STORAGE
########################################################
-->
<property name="log_path" value="${EXTERNAL_STORAGE}/neusoft/mdm/log/" />
<appender
name="File"
class="ch.qos.logback.core.rolling.RollingFileAppender" >
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss} [%thread] [%.-1level] - %msg %n</pattern>
</encoder>
<File>${log_path:-}agent.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
<FileNamePattern>${log_path:-}agent.%d{yyyy-MM-dd}.zip</FileNamePattern>
<!-- keep 7 days' worth of history -->
<MaxHistory>7</MaxHistory>
<TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP" >
<MaxFileSize>10MB</MaxFileSize><!-- MaxFileSize should be just above daily log size -->
</TimeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="TRACE" >
<!-- <appender-ref ref="LOGCAT" /> -->
<appender-ref ref="File" />
</root>
</configuration>
from logback-android.
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
from logback-android.
Related Issues (20)
- How to append Build Version Number with Log Main File HOT 1
- Document usage compatible with the standard JUnit tests
- write log after app killed ,how to write log real time HOT 1
- Log file is not creating at very first time when app gets launched in android. HOT 5
- No log is printed with Slf4j 2.x.x HOT 10
- Missing SSL parameter configuration HOT 1
- Dependency Dashboard
- how to rename locback.xml as logback-demo.xml HOT 1
- Caused by java.lang.NullPointerException Attempt to invoke interface method 'void androidx.appcompat.view.menu.d.u(int)' on a null object reference HOT 1
- ArrayIndexOutOfBoundsException on Android 10
- LogcatAppender show errors for logs' "highlighting".
- Lazy setting does not work
- Can't Create Logger: DateTokenConverter not found? HOT 2
- The log cannot be written correctly in the case of multi-process Android application? HOT 2
- Cannot create AAR with ./scripts/makejar.sh HOT 1
- Excepton in kotlin app with jetpack compose, only in release mode HOT 1
- Missing transitive dependency on slf4j-api-2.x
- The release version turns on minifyEnabled, and the file name is printed as null. <pattern>[%date] [%file:%line] - %msg%n</pattern>
- logback-android and logback-classic ILoggingEvent binary incompatibility getMarkers vs getMarkerList HOT 1
- can not write to the file that under the android getExternalFilesDir()
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from logback-android.