search

Thursday, February 18, 2016

Set up logging with Flask and gunicorn

Let's say that we want to be able to log in the specified log file and syslog. Our application - is a simple app.py file. The first step, I suggest, is to set logger name:
import os

from flask import Flask


app = Flask(__name__)
app.logger_name = "flask.app"

# ...

if __name__ == "__main__":
    app.run()
You can log in your project like this:
app.logger.info("Hello")
A log configuration file for the gunicorn may look something like this:
[loggers]
keys=root, gunicorn.error, gunicorn.access, requests.packages.urllib3.connectionpool, __main__

[handlers]
keys=log_file, syslog

[formatters]
keys=generic

[logger_root]
level=INFO
handlers=log_file, syslog

[logger___main__]
level=DEBUG
handlers=log_file, syslog
propagate=0
qualname=__main__

[logger_gunicorn.error]
level=INFO
handlers=log_file, syslog
propagate=0
qualname=gunicorn.error

[logger_gunicorn.access]
level=INFO
handlers=log_file, syslog
propagate=0
qualname=gunicorn.access

[logger_requests.packages.urllib3.connectionpool]
level=WARN
handlers=log_file, syslog
propagate=0
qualname=requests.packages.urllib3.connectionpool

[handler_syslog]
class=logging.handlers.SysLogHandler
formatter=generic
args=()

[handler_log_file]
class=logging.FileHandler
formatter=generic
args=('/home/myuser/log/gunicorn.log',)

[formatter_generic]
format=%(asctime)s [%(process)d:%(name)s:%(lineno)s] [%(levelname)s] %(message)s
datefmt=%Y-%m-%d %H:%M:%S
class=logging.Formatter
The last step is starting gunicorn:
gunicorn app:app --log-config /home/myuser/gunicorn_logging.conf

Conditions in the Vagrantfile

Every developer could have different hardware configuration. Let's say one is ready to provision a virtual machine with 4 cpus and another - only with 2 cpus. It would be nice to handle this situation with environmental variables:
Vagrant.configure(2) do |config|
  config.vm.box = "box-cutter/debian81"

  config.vm.network "private_network", ip: "192.168.33.10"
  config.ssh.forward_agent = true

  config.vm.provider "virtualbox" do |vb|
    vb.memory = "4096"
    if ENV.has_key?('VAGRANT_CPUS')
      vb.cpus = ENV['VAGRANT_CPUS']
    else
      vb.cpus = "2"
    end
  end
end
With that config the number of the provisioned cpus can be controled via VAGRANT_CPUS environmental variable.

Gnome: How to record a screencast

Gnome 3 has in-built screencast functionality. To start record your screen simply press
Ctrl + Shift + Alt + R
The indicator (a red circle) will appear in the panel, informing you that the recording has started. Default video length is limited to 30s, what might be not enough in most cases. To adjust this parameter install dconf Editor and go to the:
org.gnome.settings-daemon.plugins.media-keys
And look for max-screencast-length to adjust.