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.

Tuesday, January 26, 2016

komodo: Use custom version of the jshint

Komodo has an outdated version of the jshint. If you want to use, for example, moz:true option, you need to upgrade it. I tried it multiple times and never succeed. The problem was not in komodo (at least that was the developers say) but in jshint. All latest versions of the jshint does not work with komodo. The latest version of the jshint that is known to work with komodo is 2.6.0. To configure komodo to use jshint 2.6.0, open File -> Preferences -> Syntax Checking -> Language: Javascript and point Custom linter to the downloaded file: The version 2.6.0 is somewhere from the early 2015 but still much better then the default one.

Monday, December 14, 2015

fedora: Disable Komodo Edit file association

Remove
text/plain=komodo-edit-9.desktop
from file
~/.local/share/applications/defaults.list

Thursday, December 10, 2015

bash: How to clean logs

One of the fastest and easiest ways is just like this:
> /var/log/syslog

bash: How to remove all *.pyc files

Remove all *.pyc files from the current directory (and subdirectories):
find . -name \*.pyc -delete

Sunday, November 15, 2015

firefox: Enable tracking protection for normal session

Firefox 42 includes very interesting feature - Tracking protection. By default it is active only in the Private mode. When the Firefox team presented a new video that described the usage of the Memory tab in the Developer toolbar, they show that the CNN website normally consumes about 40 Mb of memory and with Tracking protection on it consumes about 10 Mb. If you want to enable this feature not only for the private sessions, but also for the normal sessions, you can do that by typing about:config in the address bar and changing the value of the privacy.trackingprotection.enabled to true: There is also a really interesting setting privacy.trackingprotection.updateURL, which means, that, I believe, you can host your own tracking protection list (See shavar project, blocking list format is here). When Tracking protection has blocked some resources, it shows a shield icon near the address bar. If you open the Developer console (F12 or Ctrl+Shift+K) you can see what resources were blocked: Together with AdBlock extension it could be a nice way to improve your browsing expierence.

Change colours in byobu

Colour settings file for the byobu located in the folder ~/.byoubu, if you use tmux, it will be color.tmux:
BYOBU_DARK=black
BYOBU_LIGHT=white
BYOBU_ACCENT=cyan
BYOBU_HIGHLIGHT=cyan
MONOCHROME=white
BYOBU_ACCENT - is the colour of the window delimiter; BYOBU_HIGHLIGHT - is the colour of the current highlighted window; MONOCHROME - background colour of the window title in the bottom panel;

fedora: Automatically start application during the boot

To start an application automatically in fedora, you need to create a *.desktop file in the directory ~/.config/autostart, for example:
gedit ~/.config/autostart/flux.desktop
The content of the file can be something like:
[Desktop Entry]
Type=Application
Name=flux
Comment=xflux
Exec=/home/jsn/app/xflux -l 52.3837151 -g 4.8806328
Terminal=false

Tuesday, November 10, 2015

fedora: How to add user to sudoers

You can add user to the sudoers with command:
usermod username -a -G wheel
Log out is required.

Wednesday, October 7, 2015

How to open browser (Chrome, Firefox) on the remote machine via terminal

You can open a chrome window on the remote machine via terminal with X11 forwarding. First of all, some configuration needs to be done. Open on your machine (client) ssh configuration:
vim ~/.ssh/config
Make sure that it contains:
Host *
  ForwardX11 yes
Check that on the server (remote machine) X11 forwarding is enabled. Open this file:
vim /etc/ssh/sshd_config
and check that it contains:
X11Forwarding yes
X11DisplayOffset 10
Now you can connect from your machine to the remote server with command:
ssh -Y your_name@server
Check with (env command) that environmental variable DISPLAY is something like localhost:10.0. If not, set it with command:
export DISPLAY=localhost:10.0
To check that everything was set up correctly, run on the remote server:
xclock
You should be able to see a window of the xclock application. Now you are ready to run chrome with command:
google-chrome
I had some permission issue that prevented chrome from starting, I managed to fix it by changing user data directory:
google-chrome --user-data-dir=.chrome_profile
This also might be useful:
google-chrome --no-sandbox 
Firefox can be launched just with:
firefox 
Make sure that you have permissions for the .mozilla and .cache directory on the remote machine

git: How to change the author of the commit

If you want to change the author of the specific commit (for example the latest one), run rebase command:
git rebase -i HEAD~1
HEAD~1 points to the latest commit, but it can be a commit id or branch name. To mark a commit for edit, replace `pick` with `e`: Replace commit author:
git commit --amend --author "Your Name <your.name@mail.com>" 
Finish rebase:
git rebase --continue

Tuesday, September 15, 2015

How to unset environmental variable

For example, you set DJANGO=true environmental variable with command:
export DJANGO=true
To unset it run this command:
unset DJANGO

Sunday, September 13, 2015

How to display all available package versions in Ubuntu

To display all available package versions use next command:
apt-cache madison
For example:
vagrant@vagrant:~$ apt-cache madison redis-server
redis-server | 2:3.0.2-3~bpo8+1 | http://http.debian.net/debian/ jessie-backports/main amd64 Packages
redis-server | 2:2.8.17-1+deb8u1 | http://security.debian.org/ jessie/updates/main amd64 Packages
redis-server | 2:2.8.17-1 | http://http.debian.net/debian/ jessie/main amd64 Packages
     redis | 2:2.8.17-1 | http://http.debian.net/debian/ jessie/main Sources
     redis | 2:2.8.17-1+deb8u1 | http://security.debian.org/ jessie/updates/main Sources