B4J Question Get log output

Blueforcer

Well-Known Member
Licensed User
Longtime User
I want to show the logs on a website instead of the terminal.
Ive found Erels snippet to redirect the output to a file.
But i think its not the best way because the file will get bigger and bigger, and i sometimes have problems to read the file , while my app writes to it.
Is there a way to directly access the log output?
 

Blueforcer

Well-Known Member
Licensed User
Longtime User
I think this is not what i need (if i understand it correctly). i didnt find any function wich catch the java log output. I assume its a own logger?
My app is a non-gui app. The user control this app via B4J webapp (Websocket).
Because the user has no access to the terminal log, i need to redirect it to a textarea on the webapp.
 
Upvote 0

AHilton

Active Member
Licensed User
Longtime User
I do what you mentioned in Post #1 on several server apps without a problem. What problems do you have reading the file? How are you doing that?
 
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
I get unwanted characters like squares.


i read the file with textreader every 5s and set the textarea
B4X:
    Dim Reader As TextReader
    Reader.Initialize(File.OpenInput(File.DirApp, "logs.txt"))
    textarea.SetText(Reader.ReadAll)

can you port a example how you handle this?
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
There is a Linux command called tail which shows the last several lines of a file. Maybe you can try this using jshell.
 
Upvote 0

AHilton

Active Member
Licensed User
Longtime User
This is a sub that gets called when a user connects to a webpage served by a B4J non-ui app (the "Server Monitor") that monitors my other B4J non-ui apps. The user can specify the number of lines (showing the bottom 50 lines, for example) of the servers' logs. It just reads the log file into a list and returns x number of lines at the bottom building the output string via StringBuilder. It's something quick and easy to look at in case of a problem or to check on the server apps quickly.

To keep things tidy and fast, part of each B4J non-ui app is a BackgroundWorker class that routinely trims the log files generated by it to ~5mb. The older log entry lines get archived into a database for more detailed analysis and reporting on the functioning of all of the server apps by the Server Monitor.

B4X:
Sub ShowServerLog
    ' Server Log
    ServerLogNumberOfLines.SetVal(ShowServerLogLines)
    If File.Exists(File.DirApp & Main.DirectorySeparator & "logs", "logs.txt") = True Then
        Dim sb As StringBuilder : sb.initialize
        Dim List1 As List = File.ReadList(File.DirApp & Main.DirectorySeparator & "logs", "logs.txt")
        If List1.Size-1 > = ShowServerLogLines Then
            Dim start As Int = List1.Size - 1 - ShowServerLogLines
        Else
            Dim start As Int = 0
        End If
        Dim finish As Int = List1.Size - 1
        For i = start To finish
            sb.Append(List1.Get(i) & "</br>")
        Next
        ServerLog.SetHTML(sb.ToString)
    End If
End Sub

And this, of course, is how to redirect the log() output to file, which gets called in the Main module …

B4X:
Sub RedirectOutputToFile (Dir As String, FileName As String)
    #if RELEASE
        Dim out As OutputStream = File.OpenOutput(Dir, FileName, True) 'Set to True to append the logs
        Dim ps As JavaObject
        ps.InitializeNewInstance("java.io.PrintStream", Array(out, True, "utf8"))
        Dim jo As JavaObject
        jo.InitializeStatic("java.lang.System")
        jo.RunMethod("setOut", Array(ps))
        jo.RunMethod("setErr", Array(ps))
    #end if
End Sub
 
Upvote 0

Blueforcer

Well-Known Member
Licensed User
Longtime User
I use your example to trim the file after exeeding a size.
B4X:
    If File.Exists(File.DirApp, "logs.txt") = True Then
        Log(File.Size(File.DirApp,"logs.txt"))
        If File.Size(File.DirApp,"logs.txt")>610 Then
            Dim List1 As List = File.ReadList(File.DirApp, "logs.txt")
            If List1.Size-1 > = ShowServerLogLines Then
                Dim start As Int = List1.Size - 1 - ShowServerLogLines
            Else
                Dim start As Int = 0
            End If
            Dim finish As Int = List1.Size - 1
            Dim newList As List
            Log(List1)
            newList.Initialize
            For i = start To finish
                newList.Add(List1.Get(i))
            Next
            File.WriteList(File.DirApp, "logs.txt",newList)
        End If
    End If

But i also get the squares on the website as soon as i write to the file. and the file opened in notepad++ shows blacks NUL

i didnt use the jShell lib
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…