You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+37Lines changed: 37 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -86,6 +86,43 @@ See [Wiki](https://github.com/tony19/logback-android/wiki) for documentation.
86
86
7. Open logcat for your device (via the _Android Monitor_ tab in Android Studio).
87
87
8. Click the app menu, and select the menu-option. You should see "hello world" in logcat.
88
88
89
+
## Providing Android context externally
90
+
91
+
In order to support various [special properties](https://github.com/tony19/logback-android/wiki#special-properties-for-xml-config) the code requires
92
+
access to an Android [Context](https://developer.android.com/reference/android/content/Context) instance. By default, the framework uses a workaround based
93
+
on reflection that seems to work for the time being. However, in view of Google's [restrictions on non-SDK interfaces](https://developer.android.com/guide/app-compatibility/restrictions-non-sdk-interfaces)
94
+
this code might not work anymore. Therefore, the framework provides a special API that enables the application to provide an Android context instance that
95
+
will be used instead of the workaround. **Note:** the context instance must be provided *before* it is needed by the framework, so the best place for it
96
+
would be in the application's *onCreate* callback:
97
+
98
+
```java
99
+
public class MyApplication extends Application {
100
+
@Override
101
+
public void onCreate() {
102
+
super.onCreate();
103
+
104
+
// Assuming no logging occurs before this
105
+
AndroidContextUtils.setApplicationContext(this);
106
+
}
107
+
}
108
+
```
109
+
110
+
If an earlier initialization is required, then one might consider overriding `attachBaseContext`, although at this stage the context instance might not be
111
+
fully initialized. This might be good enough though if by the time the context is used by the framework it becomes fully initialized.
112
+
113
+
```java
114
+
publicclassMyApplicationextendsApplication {
115
+
116
+
@Override
117
+
protectedvoidattachBaseContext(Contextbase) {
118
+
super.attachBaseContext(base);
119
+
AndroidContextUtils.setApplicationContext(base);
120
+
}
121
+
}
122
+
```
123
+
124
+
*Note:* despite the fact that the method is called `setApplicationContext` - the user may use *any*`ContextWrapper` component (*Application, Activity, Service*) - the
125
+
framework will actually use the ["pure" application context](https://developer.android.com/reference/android/content/Context#getApplicationContext()).
0 commit comments