Android Question using a custom file log


Active Member
Licensed User
Longtime User

I want to write my own log file, so i can manage it as I like

I need some suggestion to do "the right thing"

Sub WriteLog(Message As String)
    ' log "standard", used by b4a ide
    ' my own log file

    Dim TextWriter1 As TextWriter
    TextWriter1.Initialize(File.OpenOutput(File.DirRootExternal, "myLog.txt", True))

End Sub

I will write several log rows every second, in append mode (parameter TRUE in File.OpenOutput

my log file will grow up to 1 meg, than i will copy to a .OLD file and open a new one

--- now the nice question

is it OK to re-initialize and re-open the file every time I write (append) something, or it's better to open it at the app start and keep it open?

if it's NOT time and resource consuming, I prefer to open and close every time I write

easy question, I think

thank you


Active Member
Licensed User
I would definitely prefer to use an SQLite database for storing logs. It’s already integrated into B4X, easy to manage, exceptionally fast, and most importantly, it allows structured data storage in tables:
- proces/module ID
- Error

This will help you to find bug(s) in the app... In any case - I think that you can open/close txt when you need to read/writte data... If the application crashes and the text file remains open all the time, it will stay open… 😉
Upvote 0


Well-Known Member
Licensed User
Longtime User
I'm using a similar approach, even if I'm currently opening the file at app startup and closing it at the end: as I'm coming from an era when opening and closing files where time consuming, I never considered the open/write/close approach.
I think, in the flash memory era, it makes sense.
Crashes does not leave the file open, but I observed that some log lines may be missing because of missing flush before the crash happened.
I think the open/write/close method may loose just the last written line before the crash.
Upvote 0