<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.sugarlabs.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Johannesponader</id>
	<title>Sugar Labs - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.sugarlabs.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Johannesponader"/>
	<link rel="alternate" type="text/html" href="https://wiki.sugarlabs.org/go/Special:Contributions/Johannesponader"/>
	<updated>2026-05-13T01:19:50Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://wiki.sugarlabs.org/index.php?title=Development_Team/Almanac/Python_Standard_Logging&amp;diff=51373</id>
		<title>Development Team/Almanac/Python Standard Logging</title>
		<link rel="alternate" type="text/html" href="https://wiki.sugarlabs.org/index.php?title=Development_Team/Almanac/Python_Standard_Logging&amp;diff=51373"/>
		<updated>2010-04-23T00:46:13Z</updated>

		<summary type="html">&lt;p&gt;Johannesponader: _logger.setLevel(logging.DEBUG)&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{Almanac}}&lt;br /&gt;
&lt;br /&gt;
=== What is the overall structure of python standard logging that I will use in Sugar? ===&lt;br /&gt;
&lt;br /&gt;
Sugar uses Python&#039;s Standard Logging&amp;lt;ref&amp;gt;[http://www.onlamp.com/pub/a/python/2005/06/02/logging.html OnLamp.com-- Python Standard Logging]&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;Python Library Reference -- [http://docs.python.org/lib/module-logging.html Logging]&amp;lt;/ref&amp;gt; pretty much without modification. The diagram below conceptualizes how logging generally works. &lt;br /&gt;
&lt;br /&gt;
[[Image: Logging-Sugar-Almanac.jpg | Logging Diagram for Python]]&lt;br /&gt;
&lt;br /&gt;
=== How do I write to a log in my activity code? ===&lt;br /&gt;
Sugar uses [http://www.onlamp.com/pub/a/python/2005/06/02/logging.html python&#039;s standard logging]. The following code shows how to output something at the debug log level. The log message will be written to the standard log output destination (usually the console). &lt;br /&gt;
&lt;br /&gt;
    import logging&lt;br /&gt;
    _logger = logging.getLogger(&#039;annotate-activity&#039;)&lt;br /&gt;
    _logger.setLevel(logging.DEBUG)&lt;br /&gt;
&lt;br /&gt;
    ...&lt;br /&gt;
         _logger.debug(&#039;starting activity&#039;)&lt;br /&gt;
&lt;br /&gt;
=== How do I send my log output to multiple destinations, including a file and the console? ===&lt;br /&gt;
&lt;br /&gt;
The code example below sets up the activity to output its logs to several destinations. In particular, we output to a file called &#039;Annotate.activity.log&#039; in sugar&#039;s standard logging directory and to the console. There are many [http://docs.python.org/lib/node409.html different types of handler objects] that you can output to at the same time, depending upon your debugging and tracking needs. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from sugar import logger&lt;br /&gt;
import logging&lt;br /&gt;
import logging.config&lt;br /&gt;
_logger = logging.getLogger(&#039;activity-annotate&#039;)&lt;br /&gt;
_logger.setLevel(logging.DEBUG)&lt;br /&gt;
...&lt;br /&gt;
    #### Method: __init__, initialize this AnnotateActivity instance&lt;br /&gt;
    def __init__(self, handle):&lt;br /&gt;
        activity.Activity.__init__(self, handle)&lt;br /&gt;
&lt;br /&gt;
        #Get the standard logging directory. &lt;br /&gt;
        std_log_dir = logger.get_logs_dir()&lt;br /&gt;
&lt;br /&gt;
        #First log handler: outputs to a file called &#039;Annotate.activity.log&#039;&lt;br /&gt;
        file_handler = logging.FileHandler(os.path.join(std_log_dir, &#039;Annotate.activity.log&#039;))&lt;br /&gt;
        file_formatter = logging.Formatter(&#039;%(name)s -- %(asctime)s %(levelname)s:\n %(message)s\n&#039;)&lt;br /&gt;
        file_handler.setFormatter(file_formatter)&lt;br /&gt;
        _logger.addHandler(file_handler)&lt;br /&gt;
&lt;br /&gt;
        #Second log handler: outputs to a the console, using a slightly different output structure&lt;br /&gt;
        console_handler = logging.StreamHandler()&lt;br /&gt;
        console_formatter = logging.Formatter(&#039;%(name)s %(levelname)s ||| %(message)s&#039;)&lt;br /&gt;
        console_handler.setFormatter(console_formatter)&lt;br /&gt;
        _logger.addHandler(console_handler)&lt;br /&gt;
        ...&lt;br /&gt;
        _logger.debug(&#039;Creating annotate activity UI&#039;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== How do I read the log output that I have generated in my activity code? ===&lt;br /&gt;
&lt;br /&gt;
How you read your log output depends of course on how and where you wrote your log output using Python&#039;s standard logging tools. Typically, you will want your logs to go to the console or to the standard logging directory. &lt;br /&gt;
&lt;br /&gt;
If your log is being output to the console, the best thing to do is to launch your activity from the terminal using the &#039;sugar-launch &amp;lt;activity-name&amp;gt;&#039; command. Then, all console output will be printed to the terminal. The main drawback of this method is that, since a lot of code directs log output to the console, you will have to sift through a lot of output. For example, the output below is some initial console output (including log output) from running an activity. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
[fanwar@localhost ~]$ sugar-launch annotate&lt;br /&gt;
[u&#039;sugar-activity&#039;, u&#039;annotateactivity.AnnotateActivity&#039;, &#039;-b&#039;, dbus.String(u&#039;org.laptop.AnnotateActivity&#039;, variant_level=1), &#039;-a&#039;, &#039;b10b76394c10bd511dec1f9d5ac20af5d82a27aa&#039;]&lt;br /&gt;
1215605658.958459 WARNING root: Activity directory lacks a MANIFEST file.&lt;br /&gt;
1215605659.144058 DEBUG root: *** Act b10b76394c10bd511dec1f9d5ac20af5d82a27aa, mesh instance None, scope private&lt;br /&gt;
1215605659.144233 DEBUG root: Creating a jobject.&lt;br /&gt;
1215605659.145405 DEBUG root: datastore.write&lt;br /&gt;
1215605659.153947 DEBUG root: dbus_helpers.create: 8681691c-1859-4507-85f8-33b3836a446f&lt;br /&gt;
1215605659.154164 DEBUG root: Written object 8681691c-1859-4507-85f8-33b3836a446f to the datastore.&lt;br /&gt;
activity-annotate DEBUG ||| Creating annotate activity UI&lt;br /&gt;
1215605659.154495 DEBUG activity-annotate: Creating annotate activity UI&lt;br /&gt;
activity-annotate INFO ||| In _createToolBox method&lt;br /&gt;
1215605659.154779 INFO activity-annotate: In _createToolBox method&lt;br /&gt;
1215605659.211541 WARNING root: No gtk.AccelGroup in the top level window.&lt;br /&gt;
1215605659.214685 WARNING root: No gtk.AccelGroup in the top level window.&lt;br /&gt;
activity-annotate DEBUG ||| execing _createCanvas&lt;br /&gt;
1215605659.222814 DEBUG activity-annotate: execing _createCanvas&lt;br /&gt;
activity-annotate DEBUG ||| finished creating UI&lt;br /&gt;
1215605659.244071 DEBUG activity-annotate: finished creating UI&lt;br /&gt;
1215605659.315145 DEBUG root: ActivityService.set_active: 1.&lt;br /&gt;
** (sugar-activity:5646): DEBUG: Got client ID &amp;quot;10dc3d8b3e662f5a22121560565936684800000051360010&amp;quot;&lt;br /&gt;
** (sugar-activity:5646): DEBUG: Setting initial properties&lt;br /&gt;
** (sugar-activity:5646): DEBUG: Received SaveYourself(SmSaveLocal, !Shutdown, SmInteractStyleNone, !Fast) in state idle&lt;br /&gt;
** (sugar-activity:5646): DEBUG: Sending SaveYourselfDone(True) for initial SaveYourself&lt;br /&gt;
** (sugar-activity:5646): DEBUG: Received SaveComplete message in state save-yourself-done&lt;br /&gt;
1215605661.104732 DEBUG root: Activity.save: dbus.String(u&#039;8681691c-1859-4507-85f8-33b3836a446f&#039;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case, I was primarily interested in the lines starting with activity-annotate. &lt;br /&gt;
&lt;br /&gt;
You may want to output to a designated logfile where you can control exactly what is output (so that extraneous sugar log output is not interspersed with your activity output). The entry on [[Python_Standard_Logging_in_Sugar#How_do_I_send_my_log_output_to_multiple_destinations.2C_including_a_file_and_the_console.3F | writing to multiple files]] explains how to accomplish output to a logfile. If you write to a logfile in the standard sugar log directory, you can look at your log by opening up the log activity. In the terminal, the command &#039;sugar-launch log&#039; will accomplish this. Below is a screenshot of the log activity as I look at the specific log for my Annotate activity. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Image:log-activity-almanac.jpeg|Looking at log output]]&lt;br /&gt;
&lt;br /&gt;
= Notes =&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Johannesponader</name></author>
	</entry>
	<entry>
		<id>https://wiki.sugarlabs.org/index.php?title=Talk:Development_Team/Almanac/Python_Standard_Logging&amp;diff=51372</id>
		<title>Talk:Development Team/Almanac/Python Standard Logging</title>
		<link rel="alternate" type="text/html" href="https://wiki.sugarlabs.org/index.php?title=Talk:Development_Team/Almanac/Python_Standard_Logging&amp;diff=51372"/>
		<updated>2010-04-23T00:43:23Z</updated>

		<summary type="html">&lt;p&gt;Johannesponader: Created page with &amp;#039;Hi,  please add the following information to the standard logger section:  To have debug messages displayed, you have to set the logging level to logging.DEBUG   self.logger = se…&amp;#039;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Hi,&lt;br /&gt;
&lt;br /&gt;
please add the following information to the standard logger section:&lt;br /&gt;
&lt;br /&gt;
To have debug messages displayed, you have to set the logging level to logging.DEBUG&lt;br /&gt;
&lt;br /&gt;
 self.logger = setLevel(logging.DEBUG)&lt;br /&gt;
&lt;br /&gt;
thx!&lt;br /&gt;
&lt;br /&gt;
--̃̃̃̃&lt;/div&gt;</summary>
		<author><name>Johannesponader</name></author>
	</entry>
</feed>