You are not logged in.

  • "StefanSchnell" is male
  • "StefanSchnell" started this thread

Posts: 102

Date of registration: Mar 13th 2009

Location: Oberirsen - Germany

Occupation: Senior Software Engineer

Hobbies: Programming aund Photography

  • Send private message

1

Sunday, April 26th 2009, 8:16pm

How to use log4j with Jabaco

Hello community,
log4j is the de facto standard framework in the Java world for logging. Here you find the complete manual from Ceki Gülcü. In the following code snippet you find a short implementation for Jabaco. It is a form with two buttons. The first button print out two message to the console, and the second changes the level of the logger. After changing the level, you see only one message in the console, because only the level WARN or higher.

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
'-Define Logger------------------------------------------------------- 
  Private logClick As Logger 

'-Initialize with Form_Load------------------------------------------- 
  Private Sub Form_Load() 
    Set logClick = org#apache#log4j#Logger.getLogger("Form1.Click") 
    org#apache#log4j#BasicConfigurator.configure() 
  End Sub 

'-1st button clik----------------------------------------------------- 
  Public Sub Command1_Click() 
    logClick.info("Hello World") 
    logClick.warn("Hello World") 
  End Sub 

'-2nd button click---------------------------------------------------- 
  Public Sub Command2_Click() 
    logClick.setLevel Level.WARN 
  End Sub


I think the using from log4j with Jabaco can be very lucrative, because it is very simply to control logging messages and configure it. Look at the manual to find much more possibilities about the hierarchy of message types, appenders, configurations, filters, etc. etc.

Cheers
Stefan
Visit my personal or commercial site
If you have questions or suggestions, write me an eMail or
meet me here

A1880

Intermediate

  • "A1880" is male

Posts: 500

Date of registration: Jan 1st 2009

Location: Hanover, Germany

Occupation: Software Engineer

Hobbies: Hilbert Curves

  • Send private message

2

Monday, April 27th 2009, 1:51pm

Simple(r) approach to logging

Hi Stefan,
log4j looks appropriate for big applications with dozens or even hundreds of modules where logging control is essential for sucessful support and maintenance.

For my "weekend projects", I am using a much simpler approach. Throughout my code I intersperse "trace()" commands. The trace calls write their messages to a label control on the main form, to a rich-text box or to a log file. If necessary, I use a global debug level integer variable to control the verboseness of the trace output.

Example from a VB6 project:

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
Public Sub trace4(msg As String)
    If (iTraceLevel >= 4) Or (Left(msg, 1) = "!") Then
        trace msg
    End If
End Sub

Public Sub trace5(msg As String)
    If (iTraceLevel >= 5) Or (Left(msg, 1) = "!") Then
        trace msg
    End If
End Sub

Public Sub trace(msg As String)
    Dim s As String
    If Left$(msg, 1) = "~" Then
        s = Mid$(msg, 2)
        If iLogFileNo Then Print #iLogFileNo, s
    ElseIf Left$(msg, 1) = "!" Then
        s = Mid$(msg, 2)
        WriteToConsole s
        If iLogFileNo Then Print #iLogFileNo, s
    ElseIf iTraceLevel > 0 Then
        WriteToConsole msg
        If iLogFileNo Then Print #iLogFileNo, msg
    End If
End Sub

Public Sub warning(msg As String)
    If iTraceLevel >= 1 Then
        trace "!Achtung! " & msg
    End If
    lWarningCount = lWarningCount + 1
End Sub

Public Sub WriteToConsole(s As String)
    If tsStdOut Is Nothing Then
        attachConsole
    End If
    
    If Not (tsStdOut Is Nothing) Then
        tsStdOut.WriteLine s
    End If
    
    Debug.Print cleanString(s)
End Sub


Greetings!

A1880

OlimilO

Intermediate

  • "OlimilO" is male

Posts: 277

Date of registration: Jan 18th 2009

Location: Germany

Occupation: software engineer

  • Send private message

3

Monday, April 27th 2009, 4:18pm

ListBox.ensureIndexIsVisible

Hi,

cool samples!



Quoted

to a label control on the main form, to a rich-text box or to a log file

just an annotation:



One could also use a ListBox, if you limit the message to one line.

in VB6 i use an api call after every new item to scroll the list to the last entry.



in Jabaco it is very convenient with the additional member: ensureIndexIsVisible



Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Dim i As Long
Public Sub Command1_Click()
   PerformAbigtask
End Sub
Private Sub PerformAbigtask
   'do something long lasting like reading a big file
   TraceLB "Error " & CStr(i)
   TraceLB "Error " & CStr(i)
   TraceLB "Error " & CStr(i)
End Sub
Private Sub TraceLB(msg As String)
   i = i + 1
   Me.LstTrace.AddItem msg
   Me.LstTrace.ensureIndexIsVisible(LstTrace.ListCount-1)    
   'also want to highlight it?
   ''Me.LstTrace.ListIndex = LstTrace.ListCount-1
End Sub




greetings OlimilO

Rate this thread
WoltLab Burning Board