La gestión de los logs en bash script.

Tener trazas es vital para encontrar los problemas cuando ocurren. Y ocurren. Vamos a ver hoy como hacer una gestión de creación, trazado y borrado de logs en nuestros propios scripts.

Donde se guardan los logs

Por defecto, los ficheros de log se guardan en la carpeta /var/log/X, siendo X el programa que los generó.

En mi caso, por problemas de permisos en la máquina donde los ejecuto, los guardo en /tmp.

Crear el fichero de log

Para tener más información, mis ficheros de log contienen en su nombre qué usuario es el que generó la traza.

LOG=/tmp/$APP.$(whoami).log

Mensajes de log

Cada mensaje de log, está identificado por la aplicación que lo generó, así como la hora minuto y segundo en que se generó la línea:

function log {

echo [$APP $(date +%H:%M:%S)] $@ >> $LOG

}

El uso de esta función sería:

log ‘mensaje de prueba 1. Ejecuto en directorio’ $PWD

Sentíos libros de añadir cualquier información más a la traza.

Borrar las trazas cuando exceden de un determinado tamaño.

No se puede llenar el sistema de archivos. Sobre todo cuando es una tarea periódica que ejecuta, por ejemplo todos los días.

Por ello, antes de empezar cada ejecución yo tengo una función de validar si el log ya existe, y, si excede un determinado tamaño, se borra y se empieza uno nuevo

function openLog {

LOG_LOG=$1

LOG_APP=$2

# If log is bigger than maxsize, delete it

if [ -e $LOG_LOG ] ; then

SIZE=$(stat -c %s $LOG_LOG)

if [ $SIZE -gt $MAXSIZE ] ; then

echo ‘borrando log’

rm -rf $LOG_LOG

fi;

fi;

# Create log if it doesn’t exist.

if [ ! -e $LOG_LOG ]; then

touch $LOG_LOG

fi;

}

# Create log if it doesn’t exist.

if [ ! -e $LOG ]

touch $LOG

}

Conclusiones

Es, por tanto, necesario comenzar siempre la ejecución de nuestros script abriendo el fichero de log, que lo creará si no existe o lo borrará si es demasiado grande.

Sentíos libres de utilizar y mejorar estas funciones ampliando su funcionalidad o la cantidad de información que vuelcan al sistema.

Autor: tres

http://www.linkedin.com/pub/ester-niclos-ferreras/9/887/3b4

6 opiniones en “La gestión de los logs en bash script.”

  1. Thanks for the information. We are aware of the existence of the Asynchronous GATC. But we also know that there are compatibility issue when using GATC via the "setlocalremote" parameter and then parsing log files into Urchin Software.What's your approach to this?Cheers,Holger

  2. Chris,Thank you for your words on my comments!Colleen,Yes, I get Exactly what you were/are talking about. The way to fix it, is what I was getting at. And you are correct in that everyone saw how unbalanced he was, but no one could stop him.That’s what Sheriff Dupnik said, too. No one could lock him up until AFTER he committed his awful atrocities.

  3. hey i love your blogyou got an eye for fashioni have a question though, im studying grunge n stubbled across ur blog but im curious as to know the elements of grunge n the meaning n story as why the fashion is the why it islike i know long johns were worn cause it kept them worn n the style was basically trying to not make a statement butim not quite sure of other things such as ripped demin and layering clothes and baggy clothes,if u could shed some light for me that be great =] or lead me to to somethin?<3

  4. On key to remember is that the players play so they can get endorsements and for their paycheck, however, the owners generally own teams either because they enjoy the limelight of a sports franchise or because it supplements their other business investments. One side’s (the players) motivations are primary and the other’s (the owners) are secondary. Yes, you can point to Jerry Buss, but using him as an example destroys most of the other owner’s arguments in this negotiation.

Responder a http://www./ Cancelar la respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *