1#!/usr/bin/env tclsh 2# 3# Logging to a simple file 4# 5# This creates the file mylog.log and adds a single line. 6# 7# (c) 2005 Michael Schlenker <mic42@users.sourceforge.net> 8# 9# $Id: logtofile.tcl,v 1.2 2005/09/28 03:46:37 andreas_kupries Exp $ 10# 11# 12 13 package require logger 14 15# Define a simple custom logproc 16 proc log_to_file {lvl txt} { 17 set logfile "mylog.log" 18 set msg "\[[clock format [clock seconds]]\] $txt" 19 set f [open $logfile {WRONLY CREAT APPEND}] ;# instead of "a" 20 fconfigure $f -encoding utf-8 21 puts $f $msg 22 close $f 23 } 24 25# Initialize the logger 26 set log [logger::init global] 27 28# Install the logproc for all levels 29 foreach lvl [logger::levels] { 30 interp alias {} log_to_file_$lvl {} log_to_file $lvl 31 ${log}::logproc $lvl log_to_file_$lvl 32 } 33 34# Send a simple message to the logfile 35 ${log}::info "Logging to a file"