Python Logging Annotation – Automatically create logger for methods

This is a pattern for creating an annotation that will “inject” a logger into a python method. This allows you to decorate a python method with @logged and you will have a variable named logger created with your method name.

This is nice because you do not have to have boilerplate code all over the place.  It also allows you to easily have a logger for each function instead of a global logger.  This makes reading logs much easier since you can tell exactly where your logging call was made.

Here is an outline of the process, the comments in the code explain where these steps happen:

  1. Get the global variables for the decorated function
  2. If there is an existing logger in the function, we save it
  3. Create a new logger with the function name as the logger name
  4. Set the new logger as a global variable for the function
  5. Call the function
  6. Restore the existing logger as a global

Now your methods will look like this:

Eclipse Luna ‘Build Automatically’ is checked but not working

Eclipse Luna – had “Build Automatically” checked when Eclipse stopped compiling my classes automatically. Finally figured out that the .project file was missing an entry for the javabuilder, so I opened it and added:

Then it worked.

DJango Web Servcie

For a web service that is going to be called from an external site, we need to add the CORS headers to make sure the browser will allow it:

Lighttpd server with reverse proxy to Django

Needed a server setup so that I could run LightTPD and proxy the Django specific requests. This allows me to server static content with LightTPD and use Django for the dynamic stuff. It requires running 2 web servers, with the LightTPD forwarding the requests to the Django server when needed.

First, need to install LightTPD and Django on the systems. I’m using Windows, but it works on Linux too. I have LightTPD installed at:

d:\lighttpd

First, edit the LightTPD (d:\lighttpd\conf\lighttpd.conf on my system) configuration to server pages on port 8100:

## bind to port (default: 80)
server.port = 8100

Then, enable mod_proxy by uncommenting the “mod_proxy” line in the server.modules directive:

server.modules = (
“mod_access”,
“mod_accesslog”,
“mod_cgi”,
…………
“mod_proxy”
)

Next, we need to tell our LightTPD server which URLs need to be sent to Django and not served locally, this is done with the following – Note that I’m using http://localhost/django to indicate the request is for Django:

This says any url matching regex “^/django” should be forwarded to the server running on the same host on port 8000 (remember LightTPD is running on 8100).

Now, anything going to http://localhost/pmo will get forwarded to Django. The next issue we run into is that Django sees the url as http://localhost/django because we are not re-writing the urls. We need to setup the urls in Django so that it responds to /pmo instead of /. This is done by setting up your urls like this instead of the default:

Now, run your Django server and go to the URL http://localhost:8100/django and your Django site will work as advertised.

After this is working, we need to setup our static files to be served out of /static from LightTPD. In your htdocs directory, create a folder called static. This is where we will put all of our static files that Django needs.

Next, go to Django and modify your settings.py to be like so:

STATIC_URL is the URL that is created from the {% static %} tag in Django. Note that by setting it to /static (and not /django/static) Django will not serve this file. It will be served by LightTPD (it does not match the pattern to proxy the request).
STATIC_ROOT is the directory that the manage script will assemble all your static resources in. You could use a temp directory and then copy them to your web server, but since my server is local I just use the local directory.
STATICFILES_DIRS is the usual Django list of directories where your static files reside.

Now, we run:

python manage.py collectstatic

This will cause Django to copy all static files from STATICFILES_DIRS and the default Django static files to your STATIC_ROOT so that LightTPD will have them to serve.

Once that is done, start your Django and LightTPD server and navigate to http://localhost:8100/django/admin/ and you should see your Django site!

Here is a link to the django app:

Bootstrap Horizontal Form

PowerShell Write-Host versus Write-Output

Working with PowerShell, I had a program that was scheduled and was using Write-Host to print messages. This works great if you’re running it in the terminal, but it is not redirectable.

This means that if you schedule it and redirect the output (powershell.exe output-demo.ps1 >out.txt), you get nothing.

However, Write-Host for a comma separated list writes all of the list items on one line. Write-Output puts a newline in between. Take a look at this script:

Here is the output:

The first line is the Write-Host and the next 2 are from the Write-Output. I don’t know why this is and will update with more information.

C# Process.Start with hidden terminal window

Having issues getting the System.Disagnostics.Process.Start(…) method to run a process and hide the window. I tried all sorts of things including:

Finally came across this page on the msdn site that says:

If the UseShellExecute property is true or the UserName and Password properties are not Nothing, the CreateNoWindow property value is ignored and a new window is created.

I’m using the method:

Start(String command, String arguments)

However, there is another method that is:

Start(String command, String arguments, String user, SecureString password, String domain)

So, if we change the last line from:

p = Process.Start(proc, args);

to:

p = Process.Start(proc, args, null, null, null);

it works! Here is the complete code listing:

Spring Date converter using @InitBinder annotation

Here’s code for a controller to allow Spring to process dates properly.