在 Dancer 中重定向和恢复 STDERR
Redirect and Restore STDERR in Dancer
开始我的 http server
时,我不想在 stderr
上看到 >> Dancer2 v0.201000 server <pid> listening on http://0.0.0.0:<port>
。这就是为什么我在调用 start()
之前添加了以下行
get "/pwd" => sub {
my $pwd = cwd;
print STDERR "\n\n[PWD] : $pwd\n"; # this line is not being printed
print "\n\n[STDOUT::PWD] : $pwd\n";
my %responseHash = ( pwd => $pwd );
my $response = encode_json \%responseHash;
return $response;
};
my $dancerStartErr;
sub startServer {
open (local *STDERR, ">", $dancerStartErr)
or die "Dup err to variable error: $!\n";
start();
}
startServer();
问题是后来我无法在 STERR
上打印一些东西。我怎样才能重新打开 STDERR
(open(STDERR, ">", \*STDERR);
没有帮助)?
您可以在重定向之前使用 select 将其保存在变量中
my $oldfh = select(STDERR);
以后再用
select($oldfh);
另请查看:
如果您不希望您的应用程序记录任何内容,您可以更改日志引擎以使用 Dancer2::Logger::Null。您可以通过编辑 config.yml 或在您的某个环境中来做到这一点。例如,要在生产中关闭它,更改 # appdir/environments/production.yml.
logger: 'null'
默认是日志记录引擎'console'
,它会向您的终端打印内容。
还有其他 Dancer2::Logger:: 类 可用 bundled with Dancer2 and on CPAN in their own distributions. A better solution to just dumping everything into a black hole might be to log to a file instead. Documentation of how to configure it further can be found in Dancer2::Core::Role::Logger.
另请注意,您不应在代码中打印到 STDERR
,而应使用具有适当日志级别的日志记录关键字。
print STDERR "\n\n[PWD] : $pwd\n"; # this line is not being printed
这不是一个好主意,因为您无法区分这是错误、警告还是只是调试输出。这就是为什么 Dancer2 中内置了不同的日志级别。
core
debug
info
warning
error
所有这些都可以用作关键字。 Dancer2::Manual.
中有相关文档
由于工作目录在生产中可能不相关,而仅在开发期间,因此您会选择 debug
。
debug "[PWD] : $pwd";
就是这样。它会自动为您处理换行符等。
开始我的 http server
时,我不想在 stderr
上看到 >> Dancer2 v0.201000 server <pid> listening on http://0.0.0.0:<port>
。这就是为什么我在调用 start()
get "/pwd" => sub {
my $pwd = cwd;
print STDERR "\n\n[PWD] : $pwd\n"; # this line is not being printed
print "\n\n[STDOUT::PWD] : $pwd\n";
my %responseHash = ( pwd => $pwd );
my $response = encode_json \%responseHash;
return $response;
};
my $dancerStartErr;
sub startServer {
open (local *STDERR, ">", $dancerStartErr)
or die "Dup err to variable error: $!\n";
start();
}
startServer();
问题是后来我无法在 STERR
上打印一些东西。我怎样才能重新打开 STDERR
(open(STDERR, ">", \*STDERR);
没有帮助)?
您可以在重定向之前使用 select 将其保存在变量中
my $oldfh = select(STDERR);
以后再用
select($oldfh);
另请查看:
如果您不希望您的应用程序记录任何内容,您可以更改日志引擎以使用 Dancer2::Logger::Null。您可以通过编辑 config.yml 或在您的某个环境中来做到这一点。例如,要在生产中关闭它,更改 # appdir/environments/production.yml.
logger: 'null'
默认是日志记录引擎'console'
,它会向您的终端打印内容。
还有其他 Dancer2::Logger:: 类 可用 bundled with Dancer2 and on CPAN in their own distributions. A better solution to just dumping everything into a black hole might be to log to a file instead. Documentation of how to configure it further can be found in Dancer2::Core::Role::Logger.
另请注意,您不应在代码中打印到 STDERR
,而应使用具有适当日志级别的日志记录关键字。
print STDERR "\n\n[PWD] : $pwd\n"; # this line is not being printed
这不是一个好主意,因为您无法区分这是错误、警告还是只是调试输出。这就是为什么 Dancer2 中内置了不同的日志级别。
core
debug
info
warning
error
所有这些都可以用作关键字。 Dancer2::Manual.
中有相关文档由于工作目录在生产中可能不相关,而仅在开发期间,因此您会选择 debug
。
debug "[PWD] : $pwd";
就是这样。它会自动为您处理换行符等。