← Retour
#!/bin/bash
# ===========================================
# Affiche les logs d'un container
# Usage: ./logs.sh <container> [options]
# ===========================================
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../lib/config.sh"
source "$SCRIPT_DIR/../lib/colors.sh"
if [ -z "$1" ]; then
echo "Usage: $0 <container> [options]"
echo ""
echo "Options:"
echo " -f, --follow Suivre les logs en temps réel"
echo " -n, --tail N Afficher les N dernières lignes (défaut: 100)"
echo " --since TIME Logs depuis TIME (ex: 1h, 30m, 2023-12-01)"
echo ""
echo "Exemples:"
echo " $0 hellocar-api-prod"
echo " $0 hellocar-api-prod -f"
echo " $0 hellocar-api-prod -n 50"
echo " $0 hellocar-api-prod --since 1h"
echo ""
echo "Containers disponibles:"
docker ps --format ' - {{.Names}}' 2>/dev/null
exit 1
fi
CONTAINER="$1"
shift
# Options par défaut
TAIL="100"
FOLLOW=""
SINCE=""
# Parser les options
while [[ $# -gt 0 ]]; do
case $1 in
-f|--follow)
FOLLOW="-f"
shift
;;
-n|--tail)
TAIL="$2"
shift 2
;;
--since)
SINCE="--since $2"
shift 2
;;
*)
shift
;;
esac
done
# Vérifier si le container existe
if ! docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER}$"; then
log_error "Container '$CONTAINER' non trouvé"
echo ""
echo "Containers disponibles:"
docker ps -a --format ' - {{.Names}} ({{.Status}})'
exit 1
fi
print_header "Logs: $CONTAINER"
# Afficher les logs
docker logs $FOLLOW --tail "$TAIL" $SINCE "$CONTAINER"