After using the Java log framework log4j, you will be attracted by its convenient and powerful functions. We can not only control the log output destination, but also control the daily output level for debugging and release.
In fact, in the flex
This framework is also provided. The logging API is the most basic log control framework, but most people are using the simplest trace () function.
The logging API not only provides the most basic trace function, but also provides the log target, that is, the output method. The destination configuration function is also provided. Through our log level control, we can enter
Filter out the debug information by generating some common information. In addition, you can customize log targets to expand the framework.
Important Concepts and classes:
Logger
: Provides an interface to Send Logs to a specific target, which implements the ilogger interface.
Log target
: Defines where logs will be written. Flex
Provides two types of targets: TraceTarget and minidebugtarget. Tracetarget: The logstore is output to the trace(flash, And the flashlog.txt file. You can also customize a log target.
Logging level
: Defines the log levels that can be output by the current system.
As shown in the following table. If the current level is all, all the logs in the system will be output. If it is info, the debug information higher than info will not be output. This is easy to understand.
Logging level constant (INT) |
Description |
All (0) |
Designates that messages of all logging levels shocould be logged. |
Debug (2) |
Logs internal flex
Activities. This is most useful when debugging anapplication. Select Debug Logging level to include debug, info, warn, error, and fatal Messages in your log files. |
Info (4) |
Logs general information. Select the info logging level to include info, warn, error, and fatal messages in your log files. |
Warn (6) |
Logs a message when the application encounters a problem. These Problems do not cause the application to stop running, but cocould lead To further errors. Select the warn logging level to include warn, error, And fatal messages in your log files. |
Error (8) |
Logs a message when a critical service is not available or Situation has occurred that restricts the use of the application. Select the error logging level to include error and fatal messages in Your log files. |
Fatal (1, 1000) |
Logs a message when an event occurs that results in the failure The application. Select the fatal logging level to include only fatal Messages in your log files. |
Log target filter filters
Logtarget. Filters = ["MX. rpc. *", "MX. messaging. *"];
In this example, the class and package for log output are specified. Only the classes in the MX. RPC and MX. messaging packages can output logs, ignoring other classes. It can also be in the form of mxml:
<Mx: TraceTarget id = "logtarget" includedate = "true" includetime = "true"
Includecategory = "true" includelevel = "true">
<Mx: Filters>
<Mx: array>
<Mx: String> MX. rpc. * </MX: String>
<Mx: String> MX. messaging. * </MX: String>
</MX: array>
</MX: Filters>
<! -- 0 is represents the logeventlevel. All constant. -->
<Mx: level> 0 </MX: level>
</MX: TraceTarget>
Parameter Introduction
Required dedate = "true", the output log contains the date
Includetime = "true", output log with time
Includecategory = "true". The output log contains the category information, that is, the log output by a class or control.
Includelevel = "true" indicates whether the output log contains level information, such as [info] and [debug.
Here is a practical example.
<? XML version = "1.0" encoding = "UTF-8"?>
<Mx: Application xmlns: MX ="
Http://www.adobe.com/2006/mxml
"Layout =" absolute "initialize =" initlog () ">
<Mx: SCRIPT>
<! [CDATA [
Import MX. Logging .*;
Import mx.logging.tar gets .*;
Private var mylogger: ilogger;
Public Function printlog (Level: Number): void
{
If (Level = 2)
Mylogger. debug ("this is debug click ");
If (Level = 4)
Mylogger.info ("this is info click ");
If (Level = 6)
Mylogger. Warn ("this is warn click ");
If (Level = 8)
Mylogger. Error ("this is error click ");
If (Level = 1000)
Mylogger. Fatal ("this is fatal click ");
}
Private function initlog (): void {
/*
// Create a target.
VaR logtarget: TraceTarget = new TraceTarget ();
// Log only messages for the classes in the MX. rpc. * and
// MX. messaging packages.
Logtarget. Filters = ["*"];
// Log all log levels.
Logtarget. Level = logeventlevel. All;
// Add date, time, category, and log level to the output.
Logtarget. includedate = true;
Logtarget. includetime = true;
Logtarget. includecategory = true;
Logtarget. includelevel = true;
// Begin logging.
Log. addtarget (logtarget );
*/
Mylogger = log. getlogger ("mycustomclass ");
}
]>
</MX: SCRIPT>
<Mx: TraceTarget level = "4" includedate = "true" includetime = "true"
Includecategory = "true" includelevel = "true">
<Mx: Filters>
<Mx: array>
<Mx: String> * </MX: String>
</MX: array>
</MX: Filters>
</MX: TraceTarget>
<Mx: vbox width = "100%" Height = "100%" horizontalalign = "center" verticalalign = "Middle">
<Mx: button label = "Debug (2)" Click = "printlog (2)"/>
<Mx: button label = "Info (4)" Click = "printlog (4)"/>
<Mx: button label = "Warn (6)" Click = "printlog (6)"/>
<Mx: button label = "error (8)" Click = "printlog (8)"/>
<Mx: button label = "Fatal (1000)" Click = "printlog (1000)"/>
</MX: vbox>
</MX: Application>
This article from: http://www.javaeye.com/topic/392808