What I’m trying to do:
I created an uplink script for my anvil server, in my uplink script i created a logging function to see what the script does. But when i call functions from the anvil server, no logging entrys are produced. I even tried a simple print in one of my functions, doesn’t work ether.
As far as i know the code is executed on the uplink site or am i missing something (very new to anvil :D).
Code Example:
# code looks something like this
@anvil.server.callable
def myFunc(parameter):
log("sending back 42") #no output
print("sending back 42") #no output
return "42" #is returned to server
The Python docs have a prominent, special note regarding log and Python threads. This note may be relevant to the way Anvil treats an Uplink program’s @anvil.server.called functions.
When called from outside the Uplink program, the Anvil library starts each such function call in its own Python thread. So Python’s thread-related caveats may apply. Based on the Python docs, I suspect you’ll need to initialize the logging system before anyone can call myFunc, possibly even before your Uplink program connects to Anvil.
Anyone with actual experience in this area is invited to clarify and correct me on any/all points!
Sorry i wasn’t clear about the logging function.
The logging function i call is from a self scripted logging class that i use.
The class basically takes the message, formats it and writes it to a simple txt file.
I worked on the code again and i suddenly got an output, which is very strange.
I also found out that a lot of times when i execute the script from sublime (I’m using sublime text 3 and conda) i don’t get any lines in my log file, but when i execute the script from the terminal, i get new logging entrys every time. Which is a very strange behavior, because it sometimes works within sublime and sometimes it doesn’t.
I also changed the logging class, the functions to call the logging script are now outside the uplink class (which means i execute a logging function within the uplink class, which than calls a function outside of this class, which then calls the logging script), maybe that made a difference. So on the logging site it might be a problem with my python runtime or sublime.
But i still have no clue, why the prints suddenly work, i didn’t change them.
I keep an eye on them, if they suddenly disappear again…
Using global variables (or other global resources) from within threads very often leads to inconsistent results. Thread X may not finish its use of your logger before Thread Y resumes its use of your logger.
In this case, you (probably) don’t create threads, but the Anvil library does.
I use logging in all of my code, but I am to lazy to use real logging, so I monkey-patch bits of the python logging library over the print function.
Have you checked other folders for the contents of your logs? If you are launching you anvil uplink code from a different working directory than you think, your log will not always be where you think it is.
(From the cron, or from windows task scheduler for example)
import os
os.getcwd()
Will show you were your script is running from.
If you want my hacky logging code, Ill post it, but it is not advised to follow my methods. At the time I started doing it I was exactly replicating the functionality of code my colleague (boss who was not that into python yet) was already using in a different language.
Thanks for the advise with the os.getcwd(), i will check if sublime is executing from another directory.
If you want to, you can post your logging code, but I am actually now very pleased with my version. In combination with snippets i now only have to enter the letter “i” press enter and than enter a message and it automatically sets the function call and i was able ,with other library’s, to catch the function-, class and file-name and put it also in the logging message, which makes logging for me now very very convenient. But the code is a bit janky. For example i adjusted the class snippet in a way that automatically the logging class is imported and functions are created outside the class, so the logging function to call from inside the class gets as small as possible.
I’m also thinking about publishing my version it on github.
I took a short look into the python logging library, maybe it is a better solution to use parts of this library instead of the way i did it, i have to check this. But from a functional and “lazy programmer” view, my code is actually pretty good i guess xD (in combination with snippets)