Buchempfehlung
MySQL kurz & gut
MySQL kurz & gut
Das preiswerte Taschen- buch stellt MySQL-rele- vante Inhalte systematisch und knapp dar, sodass es sich optimal zum Nach- schlagen beim Pro- grammieren eignet. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

Tutorial

Using mdTypes [EN]

von RedakteurMODSeite 8 von 12

logging framework:
- md/lang/mdThrowable
-- md/lang/mdException
-- md/lang/mdStackTraceElement

- md/logging/mdLogger
- md/logging/mdHandler
--- md/logging/mdConsoleHandler
--- md/logging/mdFileHandler
- md/logging/mdLogRecord
- md/logging/mdFormatter
--- md/logging/mdXMLFormatter
- md/logging/mdFilter
- md/logging/mdLevel
- md/logging/mdErrorManager

To create a new logger you can do something like this:

Dim As mdLogger logger = mdLogger.getLogger("name")

The "name" can be freely set but it makes sense to use the function or class name the logger is created in.

The handlers will define where the log will be written to. Both mdHandler and mdConsoleHandler are pretty much the same. Try to use always mdConsoleHandler before mdHandler, so it is clear, that the log will be written to the console.
If you want to write to a file, use mdFileHandler instead.

But it's not only important where it will appear but also how. For this purpose we have mdFormatter and mdXMLFormatter. While the first one is a simple text output, the second one will format the output into an XML format.

To define, if a certain message is logged, we have additional possibilities. The first one is a custom filter. The logger will call the given filter and just log the message, if the filter will return TRUE. The logic after this is up to the programmer.
Another way of defining the messages is to set a certain log level with mdLevel. The default level ist mdLevel.SEVERE. This means, that only messages are logged, which are more important or equal to SEVERE. The available default level are:

ALL     = 0
FINEST  = 300
FINDER  = 400
FINE    = 500
CONFIG  = 700
INFO    = 800
WARNING = 900
SEVERE  = 1000
OFF     = &h0FFFFFFF

You can set a level to the whole logger and/or to any single handler you are using with the logger.

And here we have an example:

'Imports
#Include Once "md/util/logging/mdLogger.bi"
#Include Once "md/lang/mdException.bi"
#Include Once "md/util/logging/mdFileHandler.bi"
#Include Once "md/util/logging/mdConsoleHandler.bi"
#Include Once "md/util/logging/mdXMLFormatter.bi"

'Get a logger
Dim As mdLogger logger = mdLogger.getLogger("This")

'Create a file handler
Dim As mdFileHandler fileHandler = ExePath & "\stdout.log"

'Create a xml formatter
Dim As mdXMLFormatter xmlFormatter

'Set the xml formatter for file output
fileHandler.setFormatter(xmlFormatter)

'Add the file handler to out logger
logger.addHandler(fileHandler)

'Create another handler, this time for the console and add it also to our logger
Dim As mdConsoleHandler consoleHandler
logger.addHandler(consoleHandler)

'Now log the message "Hello logging: {0}" with the parameter "P1" and log level mdLevel.SEVERE
logger.log(mdLevel.SEVERE, "Hello logging: {0}", "P1")

'Another message, this time the paramters are in an array
Dim As String array(0 To ...) = {"P1", "P2"}
logger.log(mdLevel.SEVERE, "Hello logging: {0}, {1}", array())

'And here we log a mdException with the message "Error"
logger.log(mdLevel.SEVERE, "Hello logging", Type<mdException>("Error"))

Sleep

This example will now write to console and the given file.

All methods in these classes are declared BefehlsreferenzeintragVirtual. This means, that you can create your own custom handler, formatter, filter, whatever, extend the base class and implement your own logic! (FreeBASIC currently does not allow inheritance with Constructors, Destructors and Operators, so you will have to copy them from the base class or make custom ones.)

Writing custom filters, the mdLogRecord class is important. The filter class will get an object of mdLogRecord as a parameter. It contains all data, which is available to this log message, like the log level of this message and the exact time. Using these information, you can descide, whether this message is loggable or not.


 

Gehe zu Seite Gehe zu Seite  1  2  3  4  5  6  7  8  9  10  11  12  
Zusätzliche Informationen und Funktionen
  • Das Tutorial wurde am 17.04.2014 von RedakteurMOD angelegt.
  • Die aktuellste Version wurde am 05.09.2014 von RedakteurMOD gespeichert.
  Bearbeiten Bearbeiten  

  Versionen Versionen