B4J Question Server (HTTP(s))- bind to specific IP address? Any way to disable logs?

vcelak

Member
Licensed User
Longtime User
Hi,
is it possible to tell somehow to server module to bind only to specific IP address on multi IP computer? Is there some way to disable logs?

About LOGs - I'm running my app on ARM SoC based HW, where eMMC memory (like SD card...) is used and I does not like to have it disturbed by unnecessary log file writes. I managed it currently by way, where I created 1MB only small RAMDISK, where I pointed logs directory, so Jetty get out of space and rotates logs immediatelly after some short time - without physical disc writes. Reason why I need to remove logging permanently is to hide HTTP communication details from simple reading when the same app is ran as service on desktop PC. Also otherwise it produces about 50MB file per day for me.
 

mindful

Active Member
Licensed User
is it possible to tell somehow to server module to bind only to specific IP address on multi IP computer?
You can use this piece of code (uses JavaObject lubrary):
B4X:
'Makes the server listen/bind on the specified ip address - should be called before the server is started
Public Sub ListenOnIP(ServerObject As Server, IPAddress As String)
    Dim joServer As JavaObject = ServerObject
    joServer.SetField("host", IPAddress)
End Sub
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
is it possible to tell somehow to server module to bind only to specific IP address on multi IP computer?
Yes: https://www.b4x.com/android/forum/t...eb-server-setting-snippets.72625/#post-461793

You can set Server.LogsRetainDays to 1 to retain the logs of the last day only.

You can use this code to disable logs (please update the tips thread with it):

B4X:
   For Each h As JavaObject In GetHandlers
     If GetType(h) = "org.eclipse.jetty.server.handler.RequestLogHandler" Then
       Dim logger As Object = h.CreateEvent("org.eclipse.jetty.server.RequestLog", "logger", Null)
       h.RunMethod("setRequestLog", Array(logger))
     End If
   Next
   srvr.Start
   StartMessageLoop
End Sub

Sub Logger_Event (MethodName As String, Args() As Object) As Object
   Return Null
End Sub

The file will be created but will stay empty.

This will not affect the http logs files.
 
Upvote 0

vcelak

Member
Licensed User
Longtime User
Yes: https://www.b4x.com/android/forum/t...eb-server-setting-snippets.72625/#post-461793

You can set Server.LogsRetainDays to 1 to retain the logs of the last day only.

You can use this code to disable logs (please update the tips thread with it):

B4X:
   For Each h As JavaObject In GetHandlers
     If GetType(h) = "org.eclipse.jetty.server.handler.RequestLogHandler" Then
       Dim logger As Object = h.CreateEvent("org.eclipse.jetty.server.RequestLog", "logger", Null)
       h.RunMethod("setRequestLog", Array(logger))
     End If
   Next
   srvr.Start
   StartMessageLoop
End Sub

Sub Logger_Event (MethodName As String, Args() As Object) As Object
   Return Null
End Sub

The file will be created but will stay empty.


This will not affect the http logs files.

Hi all, I checked all this solutions and in mean time found also referenced threads. Thank you.

@Erel - there is probably missing sub in your example code reference:

B4X:
Sub GetHandlers As Object()
   Dim josrvr As JavaObject = srvr
   josrvr = josrvr.GetField("server")
   Dim jo As JavaObject = josrvr
   Do While GetType(jo) <> "org.eclipse.jetty.server.handler.HandlerCollection"
     jo = jo.RunMethodJO("getHandler", Null)
   Loop
   Return jo.RunMethod("getHandlers", Null)
End Sub
 
Upvote 0

vcelak

Member
Licensed User
Longtime User
You are correct.
Hi Erel, another thing... Code you mentioned must be run after srvr.Start method. Otherwise handlers are uninitialised, so it need to be:

B4X:
   srvr.Start
     For Each h As JavaObject In GetHandlers
     If GetType(h) = "org.eclipse.jetty.server.handler.RequestLogHandler" Then
       Dim logger As Object = h.CreateEvent("org.eclipse.jetty.server.RequestLog", "logger", Null)
       h.RunMethod("setRequestLog", Array(logger))
     End If
   Next
   StartMessageLoop
 
Upvote 0
Top