Win32下配置Log::Log4perl:文件输出UTF-8,屏幕输出CP866
Configuring Log::Log4perl for UTF-8 File Output and CP866 Console Output on Win32
To set up separate encodings for your file and screen appenders on Windows, you just need to add the encoding parameter to each appender's configuration block. Here's the complete, adjusted config:
log4perl.logger.bc_log = INFO, bc_log, console # File appender with UTF-8 encoding log4perl.appender.bc_log = Log::Log4perl::Appender::File log4perl.appender.bc_log.filename = bc.log log4perl.appender.bc_log.encoding = utf-8 log4perl.appender.bc_log.layout = Log::Log4perl::Layout::PatternLayout log4perl.appender.bc_log.layout.ConversionPattern = %d{ISO8601}%d{Z} %H %m%n # Console appender with CP866 encoding (stderr) log4perl.appender.console = Log::Log4perl::Appender::Screen log4perl.appender.console.stderr = 1 log4perl.appender.console.encoding = cp866 log4perl.appender.console.layout = Log::Log4perl::Layout::PatternLayout log4perl.appender.console.layout.ConversionPattern = %d{ISO8601}%d{Z} %H %m%n
Key Changes Explained:
- File Appender: The
encoding = utf-8line ensures thebc.logfile is written using UTF-8 encoding, preserving all characters correctly regardless of the system's default code page. - Console Appender: The
encoding = cp866line forces stderr output to use CP866, the standard code page for Cyrillic characters in Windows consoles, preventing garbled text in your terminal.
Note: Ensure you're using Log::Log4perl v1.30 or later—this version introduced full encoding support for both File and Screen appenders. You can check your module version with:
perl -MLog::Log4perl -e 'print $Log::Log4perl::VERSION'
内容的提问来源于stack exchange,提问作者gapsf
相关产品推荐
相关产品推荐

