Log Messages In Task¶
Problem¶
You want to write task messages to a persistent log file.
Solution¶
Get the reference to the Log interface by
calling get_property()
on the task and passing in TaskProperties.LOGGER
.
1l = self.get_property(TaskProperties.LOGGER)
With an instance of the Log interface, all log methods are written to the standard host log files by default. To view the log files, see Viewing Log Files.
1l.error("My text")
2l.fatal("should appear")
3l.fatal("in the file")
1from agiparallel.client import *
2from agiparallel.infrastructure import *
3from agiparallel.constants import TaskProperties
4
5
6class Task(object):
7 def execute(self):
8 l = self.get_property(TaskProperties.LOGGER)
9 l.error("My text")
10 l.fatal("should appear")
11 l.fatal("in the file")
12
13
14if __name__ == "__main__":
15 with ClusterJobScheduler("localhost") as client:
16 client.connect()
17 job = client.create_job()
18 job.add_task(Task())
19 job.submit()
20 job.wait_until_done()
Discussion¶
Logging can be important for debugging, tracing, and post-mortem analysis. The Log interface provides “free” logging within tasks. The main advantage to using the Log interface versus a custom logging mechanism is that Log is tightly integrated with the existing logging infrastructure. For instance, right clicking on a task within the monitoring applications displays the user log messages in the same logs as the host.