Snipt.net is closing on December 31st, 2016

For more information, please visit this blog post.

snipt

Ctrl+h for KB shortcuts

Go

Configurable logger [golang]

1
2
3
4
5
6
7
8
// import (
	"log"
	"os"
)

// func New(out io.Writer, prefix string, flag int) *Logger

logger = log.New(os.Stderr, "myLogger", nil)
https://snipt.net/embed/2434809517a8f5d1736de8fc981985d0/
https://snipt.net/raw/2434809517a8f5d1736de8fc981985d0/
2434809517a8f5d1736de8fc981985d0
go
Go
8
2016-12-07T19:31:10
True
False
False
Mar 29, 2013 at 03:02 PM
/api/public/snipt/58539/
configurable-logger-golang
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a> <a href="#L-8">8</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="c1">// import (</span> </span><span id="L-2"><a name="L-2"></a> <span class="s">&quot;log&quot;</span> </span><span id="L-3"><a name="L-3"></a> <span class="s">&quot;os&quot;</span> </span><span id="L-4"><a name="L-4"></a><span class="p">)</span> </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a><span class="c1">// func New(out io.Writer, prefix string, flag int) *Logger</span> </span><span id="L-7"><a name="L-7"></a> </span><span id="L-8"><a name="L-8"></a><span class="nx">logger</span> <span class="p">=</span> <span class="nx">log</span><span class="p">.</span><span class="nx">New</span><span class="p">(</span><span class="nx">os</span><span class="p">.</span><span class="nx">Stderr</span><span class="p">,</span> <span class="s">&quot;myLogger&quot;</span><span class="p">,</span> <span class="kc">nil</span><span class="p">)</span> </span></pre></div> </td></tr></table>
go, golang, log, logging

Python

small, pretty, python-to-file logging

1
2
3
4
import pprint
handle1 = open("/tmp/log.txt", "a+")
pprint.pprint(<object>, handle1)
handle1.close()
https://snipt.net/embed/f3e600461cb6c25fdf4c4c80c921bb02/
https://snipt.net/raw/f3e600461cb6c25fdf4c4c80c921bb02/
f3e600461cb6c25fdf4c4c80c921bb02
python
Python
4
2016-12-04T12:06:20
True
False
False
Jul 10, 2012 at 12:31 PM
/api/public/snipt/39838/
small-pretty-python-to-file-logging
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="kn">import</span> <span class="nn">pprint</span> </span><span id="L-2"><a name="L-2"></a><span class="n">handle1</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;/tmp/log.txt&quot;</span><span class="p">,</span> <span class="s2">&quot;a+&quot;</span><span class="p">)</span> </span><span id="L-3"><a name="L-3"></a><span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="o">&lt;</span><span class="nb">object</span><span class="o">&gt;</span><span class="p">,</span> <span class="n">handle1</span><span class="p">)</span> </span><span id="L-4"><a name="L-4"></a><span class="n">handle1</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> </span></pre></div> </td></tr></table>
logging, pprint, pretty, python

XML

ant simple logging

1
2
3
4
5
6
7
    <target name="-log-init" unless="log.initialized">
        <!-- you can set build.log.dir prior and count on immutability to preserve value -->        
        <property name="log.initialized" value="yes"/>
        <property name="build.log.dir" location="${basedir}"/>
        <record name="${build.log.dir}/build.log" action="start"/>
        <record name="${build.log.dir}/build.dbg" action="start" loglevel="debug"/>
    </target>
https://snipt.net/embed/aaaf2c44a8c421fc546dd3d2b8412526/
https://snipt.net/raw/aaaf2c44a8c421fc546dd3d2b8412526/
aaaf2c44a8c421fc546dd3d2b8412526
xml
XML
8
2016-12-07T09:00:09
True
False
False
/api/public/snipt/19389/
ant-simple-logging
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a> <span class="nt">&lt;target</span> <span class="na">name=</span><span class="s">&quot;-log-init&quot;</span> <span class="na">unless=</span><span class="s">&quot;log.initialized&quot;</span><span class="nt">&gt;</span> </span><span id="L-2"><a name="L-2"></a> <span class="c">&lt;!-- you can set build.log.dir prior and count on immutability to preserve value --&gt;</span> </span><span id="L-3"><a name="L-3"></a> <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;log.initialized&quot;</span> <span class="na">value=</span><span class="s">&quot;yes&quot;</span><span class="nt">/&gt;</span> </span><span id="L-4"><a name="L-4"></a> <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;build.log.dir&quot;</span> <span class="na">location=</span><span class="s">&quot;${basedir}&quot;</span><span class="nt">/&gt;</span> </span><span id="L-5"><a name="L-5"></a> <span class="nt">&lt;record</span> <span class="na">name=</span><span class="s">&quot;${build.log.dir}/build.log&quot;</span> <span class="na">action=</span><span class="s">&quot;start&quot;</span><span class="nt">/&gt;</span> </span><span id="L-6"><a name="L-6"></a> <span class="nt">&lt;record</span> <span class="na">name=</span><span class="s">&quot;${build.log.dir}/build.dbg&quot;</span> <span class="na">action=</span><span class="s">&quot;start&quot;</span> <span class="na">loglevel=</span><span class="s">&quot;debug&quot;</span><span class="nt">/&gt;</span> </span><span id="L-7"><a name="L-7"></a> <span class="nt">&lt;/target&gt;</span> </span></pre></div> </td></tr></table>
ant, logging

PHP

Logging in Zend

1
2
3
4
<?php
Zend_Registry::get('_LOGGER')->debug($text);
Zend_Registry::get('_LOGGER')->debug(print_r($array, true));
?>
https://snipt.net/embed/0ee4f191bb5f9ae095b1a26af3a09cca/
https://snipt.net/raw/0ee4f191bb5f9ae095b1a26af3a09cca/
0ee4f191bb5f9ae095b1a26af3a09cca
php
PHP
4
2016-12-08T17:05:36
True
False
False
/api/public/snipt/17261/
logging-in-zend
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a><span class="nx">Zend_Registry</span><span class="o">::</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;_LOGGER&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">debug</span><span class="p">(</span><span class="nv">$text</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a><span class="nx">Zend_Registry</span><span class="o">::</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;_LOGGER&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">debug</span><span class="p">(</span><span class="nb">print_r</span><span class="p">(</span><span class="nv">$array</span><span class="p">,</span> <span class="k">true</span><span class="p">));</span> </span><span id="L-4"><a name="L-4"></a><span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
logging, zend

Python

File object for logging loggers

1
2
3
4
5
To get access to underlying stream (filelike) object in Python's logging system:

logger.handlers[0].stream

You'll have to figure out which handler for a logger object you want. I've only got one in my sample code. This snipt allows one to pass this stream object into interfaces that are expecting a file object.
https://snipt.net/embed/1d8dd5f16f3b326195971ffe3b495969/
https://snipt.net/raw/1d8dd5f16f3b326195971ffe3b495969/
1d8dd5f16f3b326195971ffe3b495969
python
Python
5
2016-12-05T10:41:32
True
False
False
/api/public/snipt/10867/
file-object-for-logging-loggers
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">To</span> <span class="n">get</span> <span class="n">access</span> <span class="n">to</span> <span class="n">underlying</span> <span class="n">stream</span> <span class="p">(</span><span class="n">filelike</span><span class="p">)</span> <span class="nb">object</span> <span class="ow">in</span> <span class="n">Python</span><span class="s1">&#39;s logging system:</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="n">logger</span><span class="o">.</span><span class="n">handlers</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">stream</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="n">You</span><span class="s1">&#39;ll have to figure out which handler for a logger object you want. I&#39;</span><span class="n">ve</span> <span class="n">only</span> <span class="n">got</span> <span class="n">one</span> <span class="ow">in</span> <span class="n">my</span> <span class="n">sample</span> <span class="n">code</span><span class="o">.</span> <span class="n">This</span> <span class="n">snipt</span> <span class="n">allows</span> <span class="n">one</span> <span class="n">to</span> <span class="k">pass</span> <span class="n">this</span> <span class="n">stream</span> <span class="nb">object</span> <span class="n">into</span> <span class="n">interfaces</span> <span class="n">that</span> <span class="n">are</span> <span class="n">expecting</span> <span class="n">a</span> <span class="nb">file</span> <span class="nb">object</span><span class="o">.</span> </span></pre></div> </td></tr></table>
file, logging, stream
Copyrighted, illegal, or inappropriate content? Email [email protected].