Monolog rotating log not formatted correctly - monolog

Using monolog (3.2) with rotating logs handler.
Everything works fine BUT, the rotated logs don't use the log format I set.
The main log is fine; the dated logs use the default output format instead.
It looks like the RotatingFileHandler should inherit format from parent, but for some reason it isn't. I'm mystified.
$log = $dir . '/today.log';
$outputFormat = "[%datetime%] %level_name% > %message% %context% %extra%\n";
$dateFormat = 'Y-m-d H:i';
$line_format = new LineFormatter($outputFormat, $dateFormat);
$stream = new StreamHandler($log, Level::Debug);
$stream->setFormatter($line_format);
// u\echor($stream,'stream',STOP);
$logger -> pushHandler($stream);
$fileHandler = new RotatingFileHandler($log,5);
$logger->pushHandler($fileHandler);
...

Related

How to verify the name of a file?

I have a function that returns a file for the user avatar and I want to create tests for it. What I want to do is to check the name of the file.
This is the function:
sub get_avatar {
my $self = shift;
my $username = $self->stash('username');
my $home = Mojo::Home->new;
$home->detect('Project');
my $path = $home->child('users', 'avatars', "$username");
$path = $home->child('img', 'default.png') if !(-e $path);
$self->render_file('filepath' => $path);
}
And this is the test:
$file = $t->get_ok('/user/username/avatar')->status_is(200);
//use Data::Dumper;
//diag Dumper($file);
ok $file =~ 'username';
I want to check if the name of the $file is equivalent to 'username'(which is the name of the file from the server) or 'default' if it is a default avatar.
Don't work with the file path. Work with the actual image. Create two small test images. They can be 2x2 pixel PNG files. Maybe make them single colour, but different. Have one be your default one.
Then serialise that and put it in your test as a string. Run $t->tx->res->body through the same serialisation, and compare these two.
If you wanted, you could also make the test deploy that image before running the code, so your application doesn't depend on the image being there.

Log::Log4perl: one appender to the file in UTF-8 and another to the screen in CP866 on win32

I have Log::Log4perl config with two appenders to file and screen, like this:
log4perl.logger.bc_log = INFO, bc_log, console
log4perl.appender.bc_log = Log::Log4perl::Appender::File
log4perl.appender.bc_log.filename = bc.log
log4perl.appender.bc_log.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.bc_log.layout.ConversionPattern = %d{ISO8601}%d{Z} %H %m%n
log4perl.appender.console = Log::Log4perl::Appender::Screen
log4perl.appender.console.stderr = 1
log4perl.appender.console.layout = Log::Log4perl::Layout::PatternLayout
log4perl.appender.console.layout.ConversionPattern = %m%n
In my program all output explicitly converted to utf8 (from cp1251 for example) and somewhere in my program:
my $bc_log = Log::Log4perl->get_logger('bc_log');
$bc_log->info('Some-utf8-encoded-string');
So message goes in utf8 both to file and console, but I would like
utf8 for file and cp866 for console in one shot.
I can't figure out is it possible in Log::Log4perl?
Looks like Log::Log4perl::Appender::File has no encoding options.
I created a new appender module (by copy original Log::Log4perl::Appender::Screen) with decoding/encoding added and use it insteed of Log::Log4perl::Appender::Screen in my code.

Concrete5.7.5.2 - Where to put form file attachment headers?

I build my email headers like this:
$txt_message .= $this->txt_message;
$html_message .= $this->html_message;
$mh = Core::make('helper/mail');
$mh->to($this->email_to, $this->site_name);
$mh->from($this->email, $this->name);
$mh->replyto($this->email, $this->name);
$mh->setSubject($this->subject);
$mh->setBody($txt_message);
$mh->setBodyHtml($html_message);
#$mh->sendMail();
Some posts say an attachment can be added with
$mh->addAttachment($file);
but $file must be a file object. How can I make the uploaded file a file object?
I also found this post:http://www.adrikodde.nl/blog/2012/mail-attachments-concrete5/
But I get errors for all Zend stuff. Is Zend Mail still available in C5.7?
Where do I put headers for a file attachment? Where can I find out more about what really sends the message (is it still a Zend Mail?) and what methods are available?
Thank you.
[SOLVED]
Thanks to Nicolai, here's a working example for attaching files:
$file = $_FILES['image']['tmp_name'];
$filename = $_FILES['image']['name'];
$importer = new \Concrete\Core\File\Importer();
$file_version = $importer->import($file, $filename);
$attachment = $file_version->getFile();
$mh->addAttachment($attachment);
//Delete the file if not wanted on server
$attachment->delete();
PS. Don't forget to check the file really selected/exists/uploaded before you try to send it!
if (!empty($this->image)) {
$importer = new \Concrete\Core\File\Importer();
$image_version = $importer->import($this->image, $file_name);
if ($image_version instanceof \Concrete\Core\File\Version) {
$attachment = $image_version->getFile();
$mh->addAttachment($attachment);
}
}
#$mh->sendMail();
To add the file to your filesystem, you should take a look at this
http://concrete5.org/api/class-Concrete.Core.File.Importer.html.
On the returned object (which is a FileVersion on success), you should be able to call getFile( ) to get the actual Concrete5 File object

Plack does not come back from Catalyst

I have an app that may choose to serve a file from disk - or go to Catalyst and generate a dynamic one.
Something like this (inside call()):
if (-f $path){
my $app = Plack::App::File->new(file => $path)->to_app; #serve published page
$res = $app->($env);
}else{
log_debug "Fall through to app ";
$res = $self->app->($env);.
}
I'd like to set some cookies when it gets back. So I use Plack::Util
Plack::Util::response_cb($res, sub {
my $res = shift;
log_debug "Handling app response";
...
});
The results? In the first case (Plack::App::File), everything works as expected. In the second (continue to the app the normal way) it never comes back in.
I wonder why this is happenning? Here is my psgi initialization:
my $app = MainApp->psgi_app(#_);
$app = Plack::MyAppAbove->wrap($app);
$app = MainApp->apply_default_middlewares($app);

ZendFramework Zend_Form_Element_File setDestination vs rename filter

The code says Zend_Form_element_File::setDestination() is deprecated and to use the rename filter. However the rename filter is currently codes such that when path is set, only temporary name is given. Original filename is lost.
<?php
$file = new Zend_Form_Element_File();
$file->setDestination('/var/www/project/public');
?>
vs
<?php
$file = new Zend_Form_Element_File();
$file->addFilter('Rename', array('target' => '/var/www/project/public'));
?>
Any solution to upload files so that it preserves original filename structure but checks for existing file and appends _1.ext or _2.ext?
You need to query the name of the file after upload and then add the Rename filter then. Eg:
if ($form->file->isUploaded()) {
$fileinfo = $form->file->getFileInfo();
$filename = $fileinfo['file']['name'];
$extn = pathinfo($filename,PATHINFO_EXTENSION);
$uploadname = $this->_makeFilename($formData, $extn);
$uploadfilepath = UPLOADED_FILES_PATH . '/' . $uploadname;
$form->file->addFilter('Rename', $uploadfilepath);
$receiveStatus = $form->file->receive();
}
After submitting the form you can inspect the $_FILES['file_element']['name'] check for existing files and then set the rename filter on your form element before calling the :
$form->getValues()/isValid() or $form->file_element->receive().