Hi all,
as for the thread title, I'm in a situation where I start the server and need to show any request in the app log.
I have 3 questions related to this...
(Question 1)
Logs should be visualized on the IDE logs for developing purposes (this is part of a library), but when
the final app is compiled and started with a .bat file (that open prompt on Windows or someelse in terminal on Linux)
logs should visualized here as usually.
I have to get it as fast possible, the only way I found now is to use a Timer (set to 100 milliseconds),
every timer tick open a log file, read it as string and get modifications from last read. This is really not elegant way, it open read and close
the server log file 10 times every second.
Someone know a better way to do this ?
Is there a better way to redirect the server logs ?
Here is my actually code, it works and show on the IDE log every request, but it open a file a lots of times.
Here my log:
Here I use LogColor to show my log requests, but what happen if the log is visualized on the command prompt instead of the B4X IDE ?
It is just used as normal Log by printing all the same color ? Or may not printed at all ?
(Question 3)
I think I've found a Jetty Server bug but I'm not sure. As you can see from the code, I set GMT+2.00 in the server log preferences, to set a right time offset
(seem it default use GMT+0.00) but for some strange reasons if I start the server eg. at 1.00 of night of eg. date 10 August (here in Italy), the server
do not create a new file, it just reuse the log file for a day before, so for a day 9 August even if current date is 10 August.
Even if the server is already started, it do not create new log file at 12:00 PM when the day change.
This is a reason that in my code I check the current day log, if not exist I try to get the day after, if not exist I check the day before.
For my use the day before something is necessary, but because this is a library I have to release on the forum, I searched to adapt it for any location checking even the day after. Any way to fix it ?
Many thanks for any help.
as for the thread title, I'm in a situation where I start the server and need to show any request in the app log.
I have 3 questions related to this...
(Question 1)
Logs should be visualized on the IDE logs for developing purposes (this is part of a library), but when
the final app is compiled and started with a .bat file (that open prompt on Windows or someelse in terminal on Linux)
logs should visualized here as usually.
I have to get it as fast possible, the only way I found now is to use a Timer (set to 100 milliseconds),
every timer tick open a log file, read it as string and get modifications from last read. This is really not elegant way, it open read and close
the server log file 10 times every second.
Someone know a better way to do this ?
Is there a better way to redirect the server logs ?
Here is my actually code, it works and show on the IDE log every request, but it open a file a lots of times.
B4X:
Log("Start http server on port " & Port)
Server.Initialize("Server")
Server.Port = Port
Server.StaticFilesFolder = mRootDir
Server.LogsFileFolder = File.Combine(File.DirData("B4XWebGL"), "logs")
Server.LogsRetainDays = 1
Server.LogFormat = $"%{client}a %u %{dd/MM/yyyy HH:mm:ss ZZZ|GMT+2:00}t [%s] %r (%O Bytes) SOURCE: %{Referer}i :: %{User-Agent}i"$
Server.AddHandler("/MainPage", "MyPageHandler", False) ' Handle main page
Server.Start
TimerRequestsLog.Initialize("TimerRequestsLog", 100)
TimerRequestsLog.Enabled = True
Log($"Server started"$)
Private Sub TimerRequestsLog_Tick
Try
If mLogOldDate <> DateTime.Date(DateTime.Now) Then
mLogsFileFolder = Server.LogsFileFolder
mLogsFileName = "b4j-" & DateTime.Date(DateTime.Now) & ".request.log"
If File.Exists(mLogsFileFolder, mLogsFileName) = False Then
Dim dayAfter As String = "b4j-" & DateTime.Date(DateTime.Now + DateTime.TicksPerDay) & ".request.log"
Dim dayBefore As String = "b4j-" & DateTime.Date(DateTime.Now - DateTime.TicksPerDay) & ".request.log"
' Log(dayAfter)
' Log(dayBefore)
If File.Exists(mLogsFileFolder, dayAfter) Then
mLogsFileName = dayAfter
Else if File.Exists(mLogsFileFolder, dayBefore) Then
mLogsFileName = dayBefore
Else
LogErr("The server log file do not exist in " & mLogsFileFolder)
return
End If
End If
Log("Server Log file name: " & mLogsFileName)
Log("Server Log file path: " & File.Combine(mLogsFileFolder, mLogsFileName))
mLogOldDate = DateTime.Date(DateTime.Now)
old = File.ReadString(mLogsFileFolder, mLogsFileName)
If old.Length > 0 Then
Log(" ") : Log("INITIAL FILE LOG:")
Log(old)
Log("END OF INITIAL FILE LOG") : Log(" ")
Else
Log("Server Log File actually do not contain logs")
End If
End If
If File.Exists(mLogsFileFolder, mLogsFileName) = False Then Return
new = File.ReadString(mLogsFileFolder, mLogsFileName)
' If new.Length <> old.Length Then
' LogString = new.SubString(old.Length)
' LogColor(LogString, 0xFF0000FF)
' old = new
' End If
If new.Length <> old.Length Then
LogString = new.SubString(old.Length)
LogString = LogString.Replace(" HTTP/1.1", "")
Dim cp() As String = Regex.Split(CRLF, LogString)
For Each s As String In cp
' If s.Contains(".html") Then
Dim s2 As String = s.SubString2(0, s.LastIndexOf("SOURCE:"))
If s2.Contains(".html") Or s2.ToLowerCase.Contains("mainpage") Then
If s2.Contains("[200]") Then
LogColor(s, xui.Color_Magenta)
Else If s2.Contains("[304]") Then
LogColor(s, 0xFFFF7F00)
Else If s2.Contains("[404]") Then
LogColor(s, xui.Color_Red)
End If
Else
If s2.Contains("[200]") Then
LogColor(s, xui.Color_Blue)
Else If s2.Contains("[304]") Then
LogColor(s, 0xFFFF7F00)
Else If s2.Contains("[404]") Then
LogColor(s, xui.Color_Red)
End If
End If
Next
old = new
End If
Catch
Log(LastException.Message)
End Try
End Sub
(Question 2)Call B4XPages.GetManager.LogEvents = True to enable logging B4XPages events.
Initializing WebGL Library ...
Starting 3D engine ...
WebGL library initialized
Found 1929 file/s in 117 folder/s - Total of 2046 folders and files.
Threejs distribution folder: C:\Users\Massimo\AppData\Roaming\B4XWebGL
Threejs [build] folder: C:\Users\Massimo\AppData\Roaming\B4XWebGL\build
Threejs [library] folder: C:\Users\Massimo\AppData\Roaming\B4XWebGL\examples\jsm
Threejs [project] folder: C:\Users\Massimo\AppData\Roaming\B4XWebGL\examples
Start http server on port 8888
2024-08-05 09:41:43.395:INFOejs.Server:JavaFX Application Thread: jetty-11.0.9; built: 2022-03-30T17:44:47.085Z; git: 243a48a658a183130a8c8de353178d154ca04f04; jvm 11.0.1+13
2024-08-05 09:41:43.579:INFOejss.DefaultSessionIdManager:JavaFX Application Thread: Session workerName=node0
2024-08-05 09:41:43.606:INFOejsh.ContextHandler:JavaFX Application Thread: Started o.e.j.s.ServletContextHandler@60f408b2{/,file:///C:/Users/Massimo/AppData/Roaming/B4XWebGL/,AVAILABLE}
2024-08-05 09:41:43.616:INFOejs.RequestLogWriter:JavaFX Application Thread: Opened C:\Users\Massimo\AppData\Roaming\B4XWebGL\logs\b4j-2024_08_05.request.log
2024-08-05 09:41:44.899:INFOejs.AbstractConnector:JavaFX Application Thread: Started ServerConnector@366a57a{HTTP/1.1, (http/1.1)}{0.0.0.0:8888}
2024-08-05 09:41:44.932:INFOejs.Server:JavaFX Application Thread: Started Server@77f882fe{STARTING}[11.0.9,sto=0] @8889ms
Server started
WebGL ReadyToCode. Your WebGL code start here. URL: http://192.168.178.51:8888/examples/CubeTexture.html
crate.gif texture already exist: C:\Users\Massimo\AppData\Roaming\B4XWebGL\examples\textures\crate.gif
Already exist: C:\Users\Massimo\AppData\Roaming\B4XWebGL\examples\jsm\physics\Cloth.js
Save HTML file: C:\Users\Massimo\AppData\Roaming\B4XWebGL\examples\CubeTexture.html
Server static folder: C:\Users\Massimo\AppData\Roaming\B4XWebGL
or
Server Log file name: b4j-2024_08_05.request.log
Server Log file path: C:\Users\Massimo\AppData\Roaming\B4XWebGL\logs\b4j-2024_08_05.request.log
Server Log File actually do not contain logs
192.168.178.51 - [05/08/2024 09:41:59 +0200] [200] GET /examples/CubeTexture.html (19627 Bytes) SOURCE: - :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:32 +0200] [200] GET /examples/CubeTexture.html (19627 Bytes) SOURCE: - :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:32 +0200] [200] GET /examples/jsm/loaders/DRACOLoader.js (13628 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:32 +0200] [200] GET /examples/jsm/controls/OrbitControls.js (32250 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:32 +0200] [200] GET /examples/jsm/libs/stats.module.js (3514 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:32 +0200] [200] GET /examples/jsm/libs/lil-gui.module.min.js (29526 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:32 +0200] [200] GET /examples/jsm/loaders/GLTFLoader.js (109781 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:32 +0200] [200] GET /build/three.module.min.js (674422 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:33 +0200] [200] GET /examples/jsm/physics/Cloth.js (14943 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:33 +0200] [200] GET /examples/jsm/utils/BufferGeometryUtils.js (31554 Bytes) SOURCE: http://192.168.178.51:8888/examples/jsm/loaders/GLTFLoader.js :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:33 +0200] [200] GET /examples/jsm/loaders/FBXLoader.js (101472 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:33 +0200] [200] GET /examples/jsm/curves/NURBSCurve.js (1878 Bytes) SOURCE: http://192.168.178.51:8888/examples/jsm/loaders/FBXLoader.js :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:33 +0200] [200] GET /examples/jsm/libs/fflate.module.js (89384 Bytes) SOURCE: http://192.168.178.51:8888/examples/jsm/loaders/FBXLoader.js :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:33 +0200] [200] GET /examples/jsm/curves/NURBSUtils.js (9150 Bytes) SOURCE: http://192.168.178.51:8888/examples/jsm/curves/NURBSCurve.js :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:36 +0200] [200] GET /examples/models/fbx/Samba%20Dancing.fbx (3681360 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:36 +0200] [200] GET /examples/textures/cube/skybox/py.jpg (19634 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:36 +0200] [200] GET /examples/textures/cube/skybox/px.jpg (60050 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:36 +0200] [200] GET /examples/textures/cube/skybox/nx.jpg (55756 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:36 +0200] [200] GET /examples/textures/cube/skybox/ny.jpg (52198 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:36 +0200] [200] GET /examples/textures/cube/skybox/nz.jpg (54574 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:36 +0200] [200] GET /examples/textures/patterns/circuit_pattern2.png (6136 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:36 +0200] [200] GET /examples/textures/cube/skybox/pz.jpg (58868 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:36 +0200] [200] GET /examples/textures/terrain/grasslight-big.jpg (2595150 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:36 +0200] [200] GET /examples/textures/terrain/grasslight-big-nm.jpg (2833920 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:36 +0200] [200] GET /examples/models/gltf/Eve/eve$@walk.glb (15170672 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:38 +0200] [200] GET /favicon.ico (16958 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
192.168.178.51 - [05/08/2024 09:42:36 +0200] [200] GET /examples/models/gltf/Plant/Plant3.gltf (12108174 Bytes) SOURCE: http://192.168.178.51:8888/examples/CubeTexture.html :: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
Here I use LogColor to show my log requests, but what happen if the log is visualized on the command prompt instead of the B4X IDE ?
It is just used as normal Log by printing all the same color ? Or may not printed at all ?
(Question 3)
I think I've found a Jetty Server bug but I'm not sure. As you can see from the code, I set GMT+2.00 in the server log preferences, to set a right time offset
(seem it default use GMT+0.00) but for some strange reasons if I start the server eg. at 1.00 of night of eg. date 10 August (here in Italy), the server
do not create a new file, it just reuse the log file for a day before, so for a day 9 August even if current date is 10 August.
Even if the server is already started, it do not create new log file at 12:00 PM when the day change.
This is a reason that in my code I check the current day log, if not exist I try to get the day after, if not exist I check the day before.
For my use the day before something is necessary, but because this is a library I have to release on the forum, I searched to adapt it for any location checking even the day after. Any way to fix it ?
Many thanks for any help.
Last edited: