Wednesday, July 1, 2009

symfony and log rotation via logrotate

Here is how to configure logrotate on your symfony project. I am well aware of the pure symfony way but i prefere this one. Just an habit i guess.

Step 1: Here is the file to put in config/logrotate/mysfproject.logrotate.conf
/path/to/your/symfony/project/log/*.log {
rotate 10
daily
compress
copytruncate
missingok
}
Step 2: symlink the file in the logrotate system configuration directory
sudo ln -s /path/to/your/symfony/project/config/logrotate/mysfproject.logrotate.conf /etc/logrotate.d/


You are done!. the logs of your project will be rotated automatically every days, previous 10 days will be kept, previous days will be compressed.

Sunday, June 28, 2009

Y Criterions: Locality, Simplicity and Unicity

Long time ago, i established what i call the Y Criterions: Locality, Simplicity and Unicity. They are 3 criterions to help you design code. The name comes from : when you wonder why you do it in a way or another, see how well each alternative fits the Y criterions.
  • Locality: stuff related to each other must be close to each other
  • Simplicity: stuff must be simple
  • Unicity: stuff must be done only once

Tuesday, June 16, 2009

basic protection by htaccess

how to prevent access to several files in a given directory ? One basic practice is to prevent access to non-localhost requests. It is simple and no configuration (assuming it matches your threat model). An example i personnaly uses follows. For more details, see apache FilesMatch and mod_access.
# set the order for mod_access
Order Deny,Allow

# deny to all by default
Deny from all

# allow everything if from localhost
Allow from 127.0.0.1

# allow all the public files here
<FilesMatch "-rel-min\.js$">
Allow from all
</FilesMatch>

Sunday, June 14, 2009

php as preprocessor for javascript

I needed a preprocessor for my javascript files to have multiple environement to run them.
  • Developement: with debug
  • Test: sanity checks, maybe time measure, maybe debug
  • production: without debug and minified.
C/C++ preprocessor is well known but unknown to many javascript coders. php is used all over the web as a preprocessor for html. Moreover programming php is a skill close to programming javascript as they are both used to develop on the web. So i choosed php as it seems like a good candidat.

Put the following at the begining of your -php.js
// <?php $in_rel = in_array("--rel", $argv); ?>
// <?php $in_dev = in_array("--dev", $argv); ?>

As the filename still ends up with .js, your usual editor is more likely to detect it as javascript and handle completion/color correctly. You can even put the php tag behind a javascript comment to avoid more disturbance.

When you want to insert code specific to developpement environment:
// <?php if($in_dev): ?>
var variable_for_dev = "supervalue";
// </pre>

Early Implementation of Makefile
# very raw Makefile to preprocess the php

PREFIX=urfastr_livatar_core
SRC_JS=$(PREFIX)-php.js
DEV_JS=$(PREFIX)-dev.js
REL_JS=$(PREFIX)-rel.js
DEV_MIN_JS=$(PREFIX)-dev-min.js
REL_MIN_JS=$(PREFIX)-rel-min.js

JS_COMPRESSOR=yui-compressor

all: build_all

# inference rules to build dev/rel/min version from php one
%-dev.js : %-php.js
php -f $< -- --dev > $@;

%-rel.js : %-php.js
php -f $< -- --rel > $@;

%-min.js : %.js
$(JS_COMPRESSOR) $< > $@

build_dev: $(DEV_JS)

build_rel: $(REL_MIN_JS)

build_all: build_dev build_rel

watch: build_all
@echo "Start Watching (every second)"
while true; do sleep 1; [ -z "`find . -name $(SRC_JS) -newer $(DEV_JS)`" ] && continue; echo "Recompilation at `date`"; make; done

clean:
rm -f $(DEV_JS) $(REL_JS)
rm -f $(DEV_MIN_JS) $(REL_MIN_JS)

Saturday, June 6, 2009

Better symfony+doctrine migration with git

The current state of doctrine migration in symfony may be seen as a raw port of plain doctrine migration. The migration is "only" altering the database schema.
But with an ORM, a lot of code is tightly linked to this schema... How to handle its migration and synchronize it with the doctrine one? Here is a simple technic i use:

Say you start modifying the schema and its associated models, and this is your migration 006... Before starting create a git tag "sfmigration_pre_006". Once you completed your modification, create another git tag "sfmigration_post_006".

Now if you want to deploy to the migration 006, you simply git checkout to "sfmigration_post_006" and then migrate the database using normal symfony migration.

Wednesday, June 3, 2009

symfony, test and sf_user attributes

It is surprisingly hard to change the attributes of the user session in a functionnal test. Suppose you want to do something like
$sf_user->setAttribute('myownkey', 'myownval');

But you are in a funcitonnal test, here is how i did it
$storage = new sfSessionTestStorage(array(
'session_path' => sfConfig::get('sf_test_cache_dir')."/sessions"
));
$storage->write('symfony/user/sfUser/attributes', array(
'symfony/user/sfUser/attributes' => array(
'myownkey' =>'myownval'
)
));
$storage->shutdown();

Friday, May 29, 2009

symfony and log in prod

It may be interesting to get more informations when an issue is met in production environement. So here is a way to enable logging.


Step 1: Enable logging in prod environnement

In your apps/frontend/config/settings.yml, in the prod section, add this
prod:
.settings:
no_script_name: on
logging_enabled: on
web_debug: on

Step 2: Change the logger to log in file and web

In your apps/frontend/config/factories.yml, in the prod section, remove the default
  logger:
class: sfNoLogger
param:
level: err
loggers: ~

and Add the following
  logger:
class: sfAggregateLogger
param:
level: debug
loggers:
sf_web_debug:
class: sfWebDebugLogger
param:
level: debug
condition: %SF_WEB_DEBUG%
xdebug_logging: true
sf_file_debug:
class: sfFileLogger
param:
level: debug
file: %SF_LOG_DIR%/%SF_APP%_%SF_ENVIRONMENT%.log


Step 3: To have doctrine log too

In your config/ProjectConfiguration.class.php, you will find a function setup(), add the following line in it.
sfConfig::set('sf_debug', true);
Share/Save/Bookmark