Someone out there must have a solid function for taking a string and appending it to a log file?
Need this for a web app.
Anybody have any suggestions?
Someone out there must have a solid function for taking a string and appending it to a log file?
Need this for a web app.
Anybody have any suggestions?
This is what I wrote:
[code]Protected Sub doStringToFile(fileData As String, f As FolderItem, asBinary As Boolean = False, appendText As Boolean = False)
'convert a string back into a file
'CopyFile creates a read only file, whereas this routine creates a file that can be written to
Var tempTextOutputStream as TextOutputStream
Var tempBinaryStream As BinaryStream
'check if we are able to create the file
if f = nil then Return 'or f.ChildAt.isAlias
if f.Exists and appendText then
if asBinary then
tempBinaryStream = BinaryStream.Open(f, True)
tempBinaryStream.Write fileData
tempBinaryStream.Close
else
tempTextOutputStream = TextOutputStream.Open(f)
tempTextOutputStream.Write fileData
tempTextOutputStream.Close
end if
else
If f.Exists Then
Try
f.Remove 'remove any existing copy
Catch Error
Break
End Try
if f.Exists then Return 'don’t try to write over a file we can’t delete!
end if
if asBinary then
tempBinaryStream = BinaryStream.Create(f, True)
tempBinaryStream.Write fileData
tempBinaryStream.Close
else
tempTextOutputStream = TextOutputStream.Create(f)
tempTextOutputStream.Write fileData
tempTextOutputStream.Close
end if
end if
End Sub[/code]
sub writeLog (buffer as string, logident as String)
// Writes a log message to the log file. Message is in buffer, logident allows to identify
// where in the app the message was generated.
Var fh as FolderItem, op As TextOutputStream, nowtd As DateTime = DateTime.now, timept, logbuf As string
timept = format (nowtd.day, "0#\\ ") + app.months.value (nowtd.month) + " " + nowtd.year.ToString + format (nowtd.hour, "\\ \\ 0#\\:") + _
format (nowtd.minute, "0#\\:") + format (nowtd.second, "0#\\ \\ \\ ")
if (logident.Length>0) then logident = logident + ": "
logbuf = timept + logident + buffer
fh = new FolderItem ("/path/to/your/logfile.txt", FolderItem.PathModes.Native)
op = TextOutputStream.Open (fh)
op.WriteLine (logbuf)
op.Close ()
Thanks guys!
How would I modify the code to write it to the application directory?
Isnt this a bad idea ?
If the app crashes and user fills in info, that’s where it gets written, right?
It is if the app is running as cgi. Located next to the app, it would be accessible by anyone the on the internet if they knew the file name.
I suggest having this method save the logs outside of the directories that are made accessible by the web server. On Xojo Cloud for instance, you can put the file in a sub folder of SharedDocuments. [quote=497455:@Richard Albrecht]If the app crashes and user fills in info, that’s where it gets written, right?[/quote]
By default, thats where UnhandledException writes data, yes only because thats the only directory that the app is certain to be writable. But to be perfectly honest, you should never leave the two UnhandledException and the JavaScriptError methods blank in a web app. I suggest putting your own logging code in there and returning True so the app doesnt do that default behavior.
Well as better (and much safer and more scaleable) alternative you could use monitoring software with their HTTP APIs for app logging. WebApps are perfect for this because you can set alarms or debug stuff close to realtime without any interruptions. Check out Prometheus (https://prometheus.io/docs/introduction/overview/) or Loki (https://github.com/grafana/loki/blob/v1.5.0/docs/api.md). There are a lot of integrations for several languages - sorry not for xojo - but it’s an easy piece of cake to build your own functions. (https://prometheus.io/docs/instrumenting/clientlibs/)).