fab-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Fab-user] logging to file?


From: Nicolas Michel
Subject: Re: [Fab-user] logging to file?
Date: Thu, 26 Jan 2012 08:36:39 +0100

I think about something else : if you use pipe redirections AND that you'll have a chance to write some special unicode characters, python will raise a UnicodeDecodeError because python2 (no problem with python3) will detect that the output is not a terminal and will try to convert all string printed to ascii.
The trick is to export a special variable before executing your fab command :

export PYTHONIOENCODING=utf_8
 
If you want to try this behavior, this is what you can test. Create a python script :

#! /usr/bin/env python2
# -*- coding: UTF-8 -*-
 
print("été")

1) First try executing your code as usual:
./filename.py

Everything is OK (if your terminal and locales are in unicode)

2) Than execute your code with pipe redirection:
filename.py > a_file

You'll get a unicodedecodeerror.

3) export the special variable and redirect the stdout.
export PYTHONIOENCODING=utf_8
filename.py > a_file 

It should be OK.

Nicolas

2012/1/26 Nicolas Michel <address@hidden>
Hello,

There is another solution that takes more time and code but I use it when need something clean :
- execute your code with that : 

from fabric.context_managers import hide
 
def myjob():
  with hide('everything'):
    your code
 
- use the open python builtin with append mode ('a') to write whatever you want to a file
- you'll also probably wanted to use env.warn_only=True so you'll be able to catch errors with return_code property :
result = run("my command")
if result.return_code == 0:
  something
else:
  something

Nicolas


2012/1/25 Jeff Forcier <address@hidden>
On Wed, Jan 25, 2012 at 10:24 AM, Chris Withers <address@hidden> wrote:

> - when does fabric write to stderr versus stdout?

It tries to be "Unix-y", so normal information like "Running task 'x'"
and such goes to stdout; errors/aborts/tracebacks should go to stderr;
remote stdout/stderr are sent to the appropriate local stream; etc.

> - what happens with password prompts and the like?

Those won't work well when redirecting, no, so you'd have to ensure
your task(s) can be run noninteractively by setting Fab env vars like
env.password, using ssh keys, those sorts of things.

-Jeff

>
>
> cheers,
>
> Chris
>
> --
> Simplistix - Content Management, Batch Processing & Python Consulting
>            - http://www.simplistix.co.uk
>
> _______________________________________________
> Fab-user mailing list
> address@hidden
> https://lists.nongnu.org/mailman/listinfo/fab-user



--
Jeff Forcier
Unix sysadmin; Python/Ruby engineer
http://bitprophet.org

_______________________________________________
Fab-user mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/fab-user



--
Nicolas MICHEL



--
Nicolas MICHEL

reply via email to

[Prev in Thread] Current Thread [Next in Thread]