Skip to main content

log directory management CLI utility

Project description

log files managemant CLI utilitiy

prepare rsyslog/docker logging

/etc/docker/daemon.json

{
  "log-driver": "syslog",
  "log-opts": {
    "syslog-address": "tcp://localhost",
    "syslog-facility": "daemon",
    "tag": "container/{{.Name}}/{{.ID}}"
  }
}

/etc/rsyslog.conf

# ...
module(load="imtcp")
input(type="imtcp" port="514")
# ...
$IncludeConfig /etc/rsyslog.d/*.conf

/etc/rsyslog.d/10-container.conf

template(name="spcontainerfile" type="string" string="/var/log/container/%syslogtag:R,ERE,1,FIELD:container/([a-z0-9]*).*--end%/%$YEAR%-%$MONTH%-%$DAY%.log")
template(name="containerfile" type="string" string="/var/log/container/default/%$YEAR%-%$MONTH%-%$DAY%.log")
template(name="cmdcontainerfile" type="string" string="/var/log/container/run/%$YEAR%-%$MONTH%-%$DAY%.log")

# docker compose run
:syslogtag,contains,"-run-" ?cmdcontainerfile
& stop

# special containers
:syslogtag,startswith,"container/traefik/" ?spcontainerfile
& stop
# :syslogtag,startswith,"container/YOUR_CONTAINER_NAME/" ?spcontainerfile
# & stop

# other container logs
:syslogtag,startswith,"container/" ?containerfile
& stop

install (pip)

  • pip install log2s3
    • (optional) pip install zstd lz4 Brotli pyliblzfse
  • log2s3 [options]

install (docker)

  • docker pull ghcr.io/wtnb75/log2s3
  • docker run -u $(id -u syslog):$(id -g syslog) -v /var/log/container:/var/log/container -w /w ghcr.io/wtnb75/log2s3 [options]

subcommands

filetree

  • compress old/large log files
    • log2s3 filetree-compress --top /var/log/container --older 2d --bigger 4k --compress gzip
  • decompress all log files
    • log2s3 filetree-compress --top /var/log/container --compress decompress
  • remove old log files
    • log2s3 filetree-delete --top /var/log/container --older 30d

s3

option/environment variables

option env name description
--s3-access-key AWS_ACCESS_KEY_ID AWS Access Key
--s3-secret-key AWS_SECRET_ACCESS_KEY AWS Secret Key
--s3-region AWS_DEFAULT_REGION AWS Region
--s3-endpoint AWS_ENDPOINT_URL_S3 AWS Endpoint URL for S3
--s3-bucket AWS_S3_BUCKET AWS S3 Bucket name
--dotenv load .env for S3 client config
--prefix object key prefix
  • make bucket
    • log2s3 s3-make-bucket --s3-bucket mytestbucket123
  • list buckets
    • log2s3 s3-bucket
  • list objects
    • log2s3 s3-list
  • du
    • log2s3 s3-du
  • compress and upload to S3 object storage
    • log2s3 s3-put-tree --top /var/log/container --prefix $(hostname -s)/ --older 7d --compress xz
  • remove by object key suffix
    • log2s3 s3-delete-by-ext --prefix $(hostname -s)/ --suffix .gz

cat/view/edit

  • local files
    • log2s3 cat /path/to/file.gz
    • log2s3 less /path/to/file.xz
    • log2s3 vi /path/to/file.bz2
  • s3 objects
    • log2s3 s3-cat path/to/file.gz
    • log2s3 s3-less path/to/file.xz
    • log2s3 s3-vi path/to/file.bz2

examples

docker-compose.yml

version: '3'

services:
  logcompress:
    image: ghcr.io/wtnb75/log2s3
    container_name: logcompress
    volumes:
    - /var/log/container:/work
    user: "107:4"  # syslog:adm
    command:
    - filetree-compress
    - --top
    - /work
    - --older
    - 2d
    - --newer
    - 7d
    - --bigger
    - 4k
    - --compress
    - gzip
    profiles:
    - cli
  logstage:
    image: ghcr.io/wtnb75/log2s3
    container_name: logstage
    volumes:
    - /var/log/container:/work:ro
    command:
    - s3-put-tree
    - --top
    - /work
    - --older
    - 7d
    - --newer
    - 14d
    - --compress
    - xz
    - --prefix
    - container-logs/
    environment:
      AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY}
      AWS_SECRET_ACCESS_KEY: ${AWS_SECRET_KEY}
      AWS_ENDPOINT_URL_S3: ${AWS_ENDPOINT_URL_S3}
      AWS_REGION: ${AWS_REGION}
      AWS_S3_BUCKET: ${AWS_S3_BUCKET}
  loggc:
    image: ghcr.io/wtnb75/log2s3
    container_name: loggc
    volumes:
    - /var/log/container:/work
    user: "107:4"  # syslog:adm
    command:
    - filetree-delete
    - --top
    - /work
    - --older
    - 400d

shell script

#! /bin/sh
set -eu

# compress old
log2s3 filetree-compress --top /var/log/container --older 2d --newer 7d --bigger 4k --compress gzip

# stage older
log2s3 s3-put-tree --top /var/log/container --older 7d --newer 14d --compress xz --dotenv --s3-bucket mytestbucket123 --prefix container-log/

# remove oldest
log2s3 filetree-delete --top /var/log/container --older 400d

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

log2s3-0.2-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file log2s3-0.2-py3-none-any.whl.

File metadata

  • Download URL: log2s3-0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.1

File hashes

Hashes for log2s3-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 eaedc3ee646929cdf252b2827aa53671267ad0a00809d4676509e141f0fdb96d
MD5 a22e5cd6b632ee87f93611726d1a09e5
BLAKE2b-256 faa82e1d6225f466977bd290c371fbb10aa5b9f8500eaaf1a2512a8eed030400

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page