or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

appenders.mdbuilders.mdconfiguration.mdcontext.mdindex.mdlayouts.mdlogging.mdspi.md

configuration.mddocs/

0

# Configuration Management

1

2

Configuration management provides multiple approaches to configure Log4j 1.x compatibility layer including properties files, XML configuration, and programmatic setup.

3

4

## Configuration Classes

5

6

### PropertyConfigurator

7

8

```java { .api }

9

public class PropertyConfigurator {

10

// Configure from properties file

11

public static void configure(String configFilename);

12

public static void configure(URL configURL);

13

public static void configure(InputStream inputStream);

14

public static void configure(Properties properties);

15

16

// Configure with custom class loader

17

public static void configure(Properties properties, LoggerRepository repository);

18

19

// Watch for configuration changes

20

public static void configureAndWatch(String configFilename);

21

public static void configureAndWatch(String configFilename, long delay);

22

}

23

```

24

25

**Parameters:**

26

- `configFilename` - Path to properties configuration file

27

- `configURL` - URL pointing to configuration resource

28

- `inputStream` - InputStream containing configuration data

29

- `properties` - Properties object with configuration

30

- `repository` - LoggerRepository for configuration context

31

- `delay` - Delay in milliseconds for configuration file watching

32

33

### DOMConfigurator

34

35

```java { .api }

36

public class DOMConfigurator {

37

// Configure from XML file or resource

38

public static void configure(String filename);

39

public static void configure(URL configURL);

40

public static void configure(InputStream inputStream);

41

public static void configure(Element element);

42

43

// Configure with custom logger repository

44

public static void configure(Element element, LoggerRepository repository);

45

46

// Watch for configuration changes

47

public static void configureAndWatch(String filename);

48

public static void configureAndWatch(String filename, long delay);

49

}

50

```

51

52

**Parameters:**

53

- `filename` - Path to XML configuration file

54

- `configURL` - URL pointing to XML configuration

55

- `inputStream` - InputStream containing XML configuration

56

- `element` - DOM Element containing configuration

57

- `repository` - LoggerRepository for configuration context

58

- `delay` - Delay in milliseconds for file watching

59

60

### BasicConfigurator

61

62

```java { .api }

63

public class BasicConfigurator {

64

// Simple console configuration

65

public static void configure();

66

public static void configure(Appender appender);

67

68

// Reset configuration

69

public static void resetConfiguration();

70

}

71

```

72

73

**Parameters:**

74

- `appender` - Appender to use for basic configuration

75

76

## OptionHandler Interface

77

78

```java { .api }

79

public interface OptionHandler {

80

// Activate options after configuration

81

void activateOptions();

82

}

83

```

84

85

## Configuration Properties Format

86

87

### Basic Properties Configuration

88

```properties

89

# Root logger configuration

90

log4j.rootLogger=INFO, console, file

91

92

# Console appender

93

log4j.appender.console=org.apache.log4j.ConsoleAppender

94

log4j.appender.console.Target=System.out

95

log4j.appender.console.layout=org.apache.log4j.PatternLayout

96

log4j.appender.console.layout.ConversionPattern=%d{HH:mm:ss} %-5p %c{1} - %m%n

97

98

# File appender

99

log4j.appender.file=org.apache.log4j.FileAppender

100

log4j.appender.file.File=application.log

101

log4j.appender.file.Append=true

102

log4j.appender.file.layout=org.apache.log4j.PatternLayout

103

log4j.appender.file.layout.ConversionPattern=%d{ISO8601} [%t] %-5p %c - %m%n

104

105

# Package-specific loggers

106

log4j.logger.com.mycompany.dao=DEBUG

107

log4j.logger.org.springframework=WARN

108

log4j.logger.org.apache.http=INFO

109

```

110

111

### Rolling File Configuration

112

```properties

113

# Rolling file appender

114

log4j.appender.rolling=org.apache.log4j.RollingFileAppender

115

log4j.appender.rolling.File=app.log

116

log4j.appender.rolling.MaxFileSize=10MB

117

log4j.appender.rolling.MaxBackupIndex=5

118

log4j.appender.rolling.layout=org.apache.log4j.PatternLayout

119

log4j.appender.rolling.layout.ConversionPattern=%d %p %c - %m%n

120

```

121

122

## XML Configuration Format

123

124

### Basic XML Configuration

125

```xml

126

<?xml version="1.0" encoding="UTF-8"?>

127

<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

128

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

129

130

<!-- Console appender -->

131

<appender name="console" class="org.apache.log4j.ConsoleAppender">

132

<param name="Target" value="System.out"/>

133

<layout class="org.apache.log4j.PatternLayout">

134

<param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>

135

</layout>

136

</appender>

137

138

<!-- File appender -->

139

<appender name="file" class="org.apache.log4j.FileAppender">

140

<param name="File" value="application.log"/>

141

<param name="Append" value="true"/>

142

<layout class="org.apache.log4j.PatternLayout">

143

<param name="ConversionPattern" value="%d{ISO8601} [%t] %-5p %c - %m%n"/>

144

</layout>

145

</appender>

146

147

<!-- Package loggers -->

148

<logger name="com.mycompany.dao">

149

<level value="DEBUG"/>

150

</logger>

151

152

<logger name="org.springframework">

153

<level value="WARN"/>

154

</logger>

155

156

<!-- Root logger -->

157

<root>

158

<priority value="INFO"/>

159

<appender-ref ref="console"/>

160

<appender-ref ref="file"/>

161

</root>

162

163

</log4j:configuration>

164

```

165

166

## Programmatic Configuration

167

168

### Using PropertyConfigurator

169

```java

170

import org.apache.log4j.PropertyConfigurator;

171

import java.util.Properties;

172

173

public class ConfigurationExample {

174

public void configureLogging() {

175

// From file

176

PropertyConfigurator.configure("log4j.properties");

177

178

// From URL

179

URL configUrl = getClass().getResource("/log4j.properties");

180

PropertyConfigurator.configure(configUrl);

181

182

// From Properties object

183

Properties props = new Properties();

184

props.setProperty("log4j.rootLogger", "INFO, console");

185

props.setProperty("log4j.appender.console", "org.apache.log4j.ConsoleAppender");

186

props.setProperty("log4j.appender.console.layout", "org.apache.log4j.PatternLayout");

187

props.setProperty("log4j.appender.console.layout.ConversionPattern", "%d %-5p %c - %m%n");

188

PropertyConfigurator.configure(props);

189

}

190

}

191

```

192

193

### Using DOMConfigurator

194

```java

195

import org.apache.log4j.xml.DOMConfigurator;

196

import java.net.URL;

197

198

public class XMLConfigurationExample {

199

public void configureFromXML() {

200

// From file

201

DOMConfigurator.configure("log4j.xml");

202

203

// From URL

204

URL configUrl = getClass().getResource("/log4j.xml");

205

DOMConfigurator.configure(configUrl);

206

207

// Watch for changes

208

DOMConfigurator.configureAndWatch("log4j.xml", 60000); // Check every minute

209

}

210

}

211

```

212

213

### Using BasicConfigurator

214

```java

215

import org.apache.log4j.BasicConfigurator;

216

import org.apache.log4j.ConsoleAppender;

217

import org.apache.log4j.PatternLayout;

218

219

public class BasicConfigurationExample {

220

public void setupBasicLogging() {

221

// Simple console logging

222

BasicConfigurator.configure();

223

224

// With custom appender

225

ConsoleAppender appender = new ConsoleAppender(

226

new PatternLayout("%d %-5p %c - %m%n"),

227

ConsoleAppender.SYSTEM_OUT

228

);

229

BasicConfigurator.configure(appender);

230

}

231

}

232

```

233

234

## Configuration File Watching

235

236

### Automatic Configuration Reloading

237

```java

238

import org.apache.log4j.PropertyConfigurator;

239

import org.apache.log4j.xml.DOMConfigurator;

240

241

public class ConfigurationWatching {

242

public void setupWatching() {

243

// Watch properties file for changes every 30 seconds

244

PropertyConfigurator.configureAndWatch("log4j.properties", 30000);

245

246

// Watch XML file for changes every 60 seconds

247

DOMConfigurator.configureAndWatch("log4j.xml", 60000);

248

}

249

}

250

```

251

252

### Manual Configuration Reset

253

```java

254

import org.apache.log4j.LogManager;

255

import org.apache.log4j.BasicConfigurator;

256

257

public class ConfigurationReset {

258

public void resetAndReconfigure() {

259

// Reset all configuration

260

LogManager.resetConfiguration();

261

262

// Reconfigure with new settings

263

PropertyConfigurator.configure("new-log4j.properties");

264

265

// Or use basic configuration

266

BasicConfigurator.configure();

267

}

268

}

269

```

270

271

## Configuration Best Practices

272

273

### Resource Loading

274

```java

275

import org.apache.log4j.PropertyConfigurator;

276

import java.net.URL;

277

import java.io.InputStream;

278

279

public class ResourceConfiguration {

280

public void configureFromClasspath() {

281

// Load from classpath

282

URL configUrl = getClass().getClassLoader().getResource("log4j.properties");

283

if (configUrl != null) {

284

PropertyConfigurator.configure(configUrl);

285

}

286

287

// Load as stream

288

InputStream configStream = getClass().getResourceAsStream("/log4j.properties");

289

if (configStream != null) {

290

PropertyConfigurator.configure(configStream);

291

}

292

}

293

}

294

```