Trying log info - Why doesn't this work?

I created this Method in the App of a web application:

sub WriteLog(lsText as String)
  Dim f As FolderItem
  Dim t as TextOutputStream
  f = GetFolderItem("Log.Text")
  
  Dim ld as New Date
  If f = Nil then
    t = TextOutputStream.Create(f)
  end
  t = TextOutputStream.Append(f)
  t.WriteLine( ld.ShortDate + " " + ld.LongTime  + " - " + lsText)
  t.Close
end sub

It will only work once, after the app quits it will no longer write to the file. Nothing at all.

Anybody have any ideas?

f=nil is not the same as f.exists = false, so your code is not right.

I would write this:

 If f = Nil then
   msgbox "Bad error, couldn't open my log file"
  App.Quit
else if not f.exits then
    t = TextOutputStream.Create(f)
else
    t = TextOutputStream.Append(f)
end if

Also, that’s going to be horribly inefficient, to constantly be re-creating the TOS.

I would create or open the TOS once, then keep a reference to the TextOutputStream open while the app is running. Don’t re-open it with every log.
Use .Flush() to ensure that data gets written promptly.

I’ll give it a try.

I wasn’t really looking for efficiency, I’m try to figure out what events in a web cgi app and in what order. I have an issue where an app didn’t quit, remained in memory and finally caused an internal server error when someone tried to access it.

Its here: https://forum.xojo.com/27599-internal-server-error-because-app-won-t-quit/p1#p228108

Thanks

@Michael Diehr

Didn’t work, same thing. It will not append to file, it only happens when it is first created on the first launch.

@Richard Albrecht

If your web app runs on Linux, you could try using a shell object and just calling Echo:

Make LogShell as Shell a property on the App class.

if LogShell = nil then LogShell = New Shell End If LogShell.Execute("echo """ + msg + """ >> " + logfile.ShellPath

@Greg O’Lone

My code is OK though right?

I’ll try you suggestion.

I just tried System.Log and it does write very nicely to the system log.

This :

System.Log System.LogLevelWarning, "Hello World"

Shows in the Log viewer syslog as :

Nov 16 19:24:34 mitch-virtual-machine /tmp/Debugzozo/Debugzozo[2701]: Hello World.

Maybe that could be enough to debug your events order.

Note that update is not immediate. I found that relaunching the log viewer can speed things up.

Here’s my Logit method:

[code]Sub Logit(Msg as String)
dim f as folderitem

#if TargetXojoCloud then
f = SpecialFolder.SharedDocuments.child(“WebComm”).child(“log.txt”)
#else
f= GetFolderItem("").parent.child(“log.txt”)
#Endif

dim tos as TextOutputStream

if f.exists then
Tos = TextOutputStream.Append(f)
else
tos = TextOutputStream.create(f)
end

tos.WriteLine msg

tos.close
End Sub
[/code]

I thought of using System.Log, then decided I just wanted a simple text file in the same directory as the app.

Bob I will try yours.

[quote=230136:@Bob Keeney] #if TargetXojoCloud then
f = SpecialFolder.SharedDocuments.child(“WebComm”).child(“log.txt”)[/quote]

Bob. How do you keep multiple users from grabbing the file at the same moment?

Perhaps you don’t need to: We have been chasing bad crash in XC for a while now we think we have narrowed it to the mysql plugin but the good folks at Xojo (who went above and beyond with their support on this one) had us rewrite our login code so that we wrote to an array in App. Then had a timer spool the log file one item at a time.

The feeling was that perhaps grabbing the log file by two session was causing crashes.

This method is in the application object. And I should add that I don’t use this much for production so it’s not been stressed tested by multiple users at the same time.