Ubuntu – Apache Custom Log Format

apache-2.2log-filesPHPUbuntu

I am trying to write a reward system wherein users will be given reward points if they download complete files, So what should be my log format.

After searching alot this is what I understand its my first time and havent done custom logs before.

First of all which file should I edit for custom logs because this thing I cant find. I am using ubuntu server with default apache, php5 and mysql installation

# I use this commands and they work fine  
nano /etc/apache2/apache2.conf
/etc/init.d/apache2 restart

I think this is what I need to do for my purpose

LogLevel notice
LogFormat "%f %u %x %o" rewards
CustomLog /var/www/logs/rewards_log rewards

This is as it is command or there is something missing? and is there any particular location where I need to add this?

and one more thing %o is for filesize that was sent and is it possible to log only files from a particular directory? or for files with size more than 10mb.

Thank You.

Best Answer

Apache server docs are your friend: http://httpd.apache.org/docs/2.1/mod/mod_log_config.html#customlog

CustomLog entries are either in the global scope or virtual hosts. In Ubuntu you should have a sites-available and sites-enabled directories. If you are not using virtual hosts, this should go in the /etc/apache2/sites-availabe/default file

As far as I can tell, you can't do custom logs per directory, but that shouldn't be too hard to do with whatever you are using to parse the apache logs

Have you given any thought to using a web platform to handle downloads and also log those downloads to a database? Apache seems cumbersome for this.

Here is an example in Myna but you can do something similar in PHP, ColdFusion, etc:

example link:

    <a href="/myna/download.sjs?f=somefile.pdf">

The download.sjs page:

    // assumes a table a called "downloads" in the database "my_app" 
    // also assumes that the user has authenticated and has auth cookie

    var downloads = new Myna.DataManager("my_app").getManager("downloads")

    //clean the filename
    var filename = $req.rawData.f.replace(/\.\.\//g,"");
    //get the data...should really check for failure here
    var data = new Myna.File("file:/var/files/" + filename).readBinary();

    //log the download to the DB
    downloads.create({
        userid:$cookie.getAuthUserId(),
        filename:f,
        ts:new Date()
    })

    //send the binary data to the browser
    $res.prinBinary(data,"application/octet-stream",filename);