@@ -218,106 +218,4 @@ and `error()` methods.
218218[#CustomLoggers] 
219219== Generating Source Code for a Custom Logger Wrapper 
220220
221- Common Log4j usage is to get an instance of the `Logger` interface from 
222- the `LogManager` and call the methods on this interface. However, the 
223- custom log Levels are not known in advance, so Log4j cannot provide an 
224- interface with convenience methods for these custom log Levels. 
225- 
226- To solve this, Log4j ships with a tool that generates source code for a 
227- Logger wrapper. The generated wrapper class has convenience methods for 
228- each custom log level, making custom levels just as easy to use as the 
229- built-in levels. 
230- 
231- There are two flavors of wrappers: ones that _*extend*_ the Logger API 
232- (adding methods to the built-in levels) and ones that _*customize*_ the 
233- Logger API (replacing the built-in methods). 
234- 
235- When generating the source code for a wrapper class, you need to 
236- specify: 
237- 
238- * the fully qualified name of the class to generate 
239- * the list of custom levels to support and their `intLevel` relative 
240- strength 
241- * whether to extend `Logger` (and keep the existing built-in methods) or 
242- have only methods for the custom log levels 
243- 
244- You would then include the generated source code in the project where 
245- you want to use custom log levels. 
246- 
247- [#ExampleUsage] 
248- == Example Usage of a Generated Logger Wrapper 
249- 
250- Here is an example of how one would use a generated logger wrapper with 
251- custom levels DIAG, NOTICE and VERBOSE: 
252- 
253- [source,java] 
254- ---- 
255- // ExtLogger is a generated logger wrapper 
256- import com.mycompany.myproject.ExtLogger; 
257- 
258- public class MyService { 
259-     // instead of Logger logger = LogManager.getLogger(MyService.class): 
260-     private static final ExtLogger logger = ExtLogger.create(MyService.class); 
261- 
262-     public void demoExtendedLogger() { 
263-         // ... 
264-         logger.trace("the built-in TRACE level"); 
265-         logger.verbose("a custom level: a VERBOSE message"); 
266-         logger.debug("the built-in DEBUG level"); 
267-         logger.notice("a custom level: a NOTICE message"); 
268-         logger.info("the built-in INFO level"); 
269-         logger.diag("a custom level: a DIAG message"); 
270-         logger.warn("the built-in WARN level"); 
271-         logger.error("the built-in ERROR level"); 
272-         logger.fatal("the built-in FATAL level"); 
273-         logger.notice("java 8 lambda expression only executed if NOTICE is enabled: {}", () -> someMethod()); 
274-         // ... 
275-     } 
276-     ... 
277- } 
278- ---- 
279- 
280- [#CodeGen] 
281- == Generating Extended Loggers 
282- 
283- Use the following command to generate a logger wrapper that adds methods 
284- to the built-in ones: 
285- 
286- [source,sh,subs="attributes"] 
287- ---- 
288- java -cp log4j-core-{log4j-core-version}.jar org.apache.logging.log4j.core.tools.ExtendedLoggerGenerator \ 
289-         com.mycomp.ExtLogger DIAG=350 NOTICE=450 VERBOSE=550 > com/mycomp/ExtLogger.java 
290- ---- 
291- 
292- This will generate source code for a logger wrapper that has the 
293- convenience methods for the built-in levels _as well as_ the specified 
294- custom levels. The tool prints the generated source code to the console. 
295- By appending " > _filename_" the output can be redirected to a file. 
296- 
297- NOTE: Prior to log4j-2.9, this tool was an inner class 
298- `Generate$ExtendedLogger`. + 
299- Under the bash shell on Unix/Mac/Linux the dollar character $ needs to 
300- be escaped, so the class name should be between single quotes 
301- 'org.apache.logging.log4j.core.tools.Generate$ExtendedLogger’. 
302- 
303- == Generating Custom Loggers 
304- 
305- Use the following command to generate a logger wrapper that hides the 
306- built-in levels and has only custom levels: 
307- 
308- [source,sh,subs="attributes"] 
309- ---- 
310- java -cp log4j-core-{log4j-core-version}.jar org.apache.logging.log4j.core.tools.CustomLoggerGenerator \ 
311-         com.mycomp.MyLogger DEFCON1=350 DEFCON2=450 DEFCON3=550 > com/mycomp/MyLogger.java 
312- ---- 
313- 
314- This will generate source code for a logger wrapper that _only_ has 
315- convenience methods for the specified custom levels, _not_ for the 
316- built-in levels. The tool prints the generated source code to the 
317- console. By appending " > _filename_" the output can be redirected to a 
318- file. 
319- 
320- NOTE: Prior to log4j-2.9, this tool was an inner class `Generate$ExtendedLogger`. 
321- Under the bash shell on Unix/Mac/Linux the dollar character $ needs to 
322- be escaped, so the class name should be between single quotes 
323- 'org.apache.logging.log4j.core.tools.Generate$CustomLogger’. 
221+ In order to automatically generate custom logger wrappers, see the https://logging.apache.org/log4j/transform/latest/#log4j-codegen'[Apache Log4j code generator]. 
0 commit comments