Home » core java » Page 40

Tag Archives: core java

Prevent the logger send log messages to its parent logger

With this example we are going to demonstrate how to prevent the logger from sending log messages to its parent logger. When a Logger is used, it keeps track of a parent Logger, which is its nearest existing ancestor in the Logger namespace. By default, the logger publishes to its parent’s Handler. To prevent the logger from sending log messages ...

Read More »

Conditional logging

This is an example of how to use Conditional logging. Using conditional logging in a Class means that we check the Level for which the Logger is enabled before we log a message to that level. We have implemented a Class that uses a logger. The Class consists of a simple method that uses the logger to log messages after ...

Read More »

Use Logger MemoryHandler class

In this example we shall show you how to use the Logger MemoryHandler class. The MemoryHandler is a Handler that buffers requests in a circular buffer in memory. To use the MemoryHandler of a logger one should perform the following steps: Create a Logger instance, with the getLogger(String name) API method of the Logger. Initialize a FileHandler to write to ...

Read More »

Log an exception

With this example we are going to demonstrate how to log an exception. In order to do so, we will use a DateFormat and parse a String pattern to create a new Date. In short, to log the ParseException that occurs you should: Create a new SimpleDateFormat with a specific String pattern. Invoke the setLenient(boolean lenient) API method of the ...

Read More »

Set size threshold of log file

This is an example of how to set size threshold of a log file. Setting size threshold of a log file implies that you should: Create a Logger instance for a class, with the getLogger(String name) API method. Create a FileHandler with a specific file size. It can be configured to write to one or more files, to a specific ...

Read More »

Use of logger console handler

In this example we shall show you how to use a logger’s ConsoleHandler. The ConsoleHandler is a handler that takes logs from a Logger and publishes them to System.err. To use the ConsoleHandler one should perform the following steps: Create a Logger instance, with the getLogger(String name) API method of the Logger. Create a ConsoleHandler for System.err. Add the handler ...

Read More »

Set logger log level

With this example we are going to demonstrate how to set a Logger‘s log level. The Level defines a set of standard logging levels that can be used to control logging output. The standard levels are provided at the Level API. In short, to set logger log level you should: Create a new Logger instance. Set the log level to ...

Read More »

Create a sequence of log files

This is an example of how to create a sequence of log files. Creating a sequence of log files implies that you should: Create a new instance of a Logger. Create an instance of FileHandler with a specific number of logging files sequences, a specific size for each file and append set to true. In the example 5 files will ...

Read More »

Create custom Formatter for Logger handler

In this example we shall show you how to create custom Formatter for a Logger‘s Handler. The Handler usually uses a Formatter associated with it to format LogRecords. The Formatter takes a LogRecord and converts it to a string. LogRecord objects are used to pass logging requests between the logging framework and individual log Handlers. To create a custom Formatter ...

Read More »

Set Formatter for Logger handler

With this example we are going to demonstrate how to set a Formatter for a Logger Handler. The Formatter is used by the Handler to format LogRecords. In short, to set a Formatter for a Logger’s handler you should: Create a new Logger instance with getLogger(String name) API method of Logger. Create a new FileHandler to write to a specific ...

Read More »