How to show logging message in Python - error-logging

I need to show logging messages while debugging what should I do to show messages with the code like this:
logs.out("Here are the error messages")
logs.clear()

You should be using the logging library,
Here is an example:
import logging
logging.debug('debug message')
logging.info('information message')
logging.warning('A warning message')
logging.error('Error Description')
logging.critical('Critical messages should be here')

If you prefer to just output some common text for logging then you can opt in the print() function of Python or else of you want to log like the proper way that it should be then you can use the logging API provided by the Python itself as a library.
`
import logging
logging.basicConfig(filename='example.log', level=logging.debug)
logging.debug("Debugging")
`

Related

Send log with customDimensions to Application Insights using Powershell

I want to send logs to my Application Insights using PowerShell script and I want it to contain customDimensions value.
I tried something like this:
$client = [Microsoft.ApplicationInsights.TelemetryClient]::new()
$client.InstrumentationKey = "xxxx"
$client.TrackTrace("MY CUSTOM LOG", "Information", #{foo="bar"})
$Client.Flush()
but it gives me an error:
Cannot find an overload for "TrackTrace" and the argument count: "3".
Ideally I want the code above to work so that I can read the log like this:
I know how to do it using Python and opencensus-ext-azure lib but now I also need Powershell as well.

Run http tests until AMQ msgs consumed

We're running Gatling/Scala to load test an application in the following manner:
Post a ton of messages to AMQ.
Run a number of REST calls while messages are being consumed.
The Gatling setup looks like this:
setUp(
JmsScenario.run inject(atOnceUsers(events)) protocols(JmsScenario.jmsConnect),
HttpScenario.run inject(constantUsersPerSec(httpThroughput) during(httpDuration)) protocols(HttpScenario.protocol),
)
But instead of using during(...) I'd like to use something like untilAmqMessageQueueIsEmpty(). Is there some (fairly easy) way to accomplish this?
Use asLongAs and call some custom code (in Java if you don't know Scala).
See https://gatling.io/docs/current/general/scenario/#aslongas.
// isAmqMessageQueueIsEmpty is something only you can code
// It has to return a Boolean
asLongAs(session => isAmqMessageQueueIsEmpty()) {
???
}

CakePHP 3.x - Email to log instead of sending during debug

I would like to switch my application to a configuration where email isn't actually send, but instead saved to a log file.
This way I can test my application normally without being afraid of accidentally emailing to hundreds of users and without spamming myself.
I figured something with EmailTransports could be a solution. For instance, when using the DebugTransport the emails aren't send at all, the mail content is instead only returned by the ->send() function.
The downside of this transport is than I have to modify controller code in order to display the content, which I would like to avoid.
So is there a configuration such that email is stored to files instead of being sent, e.g.:
[root]
logs/
emails/
2019-10-01_15:32_email#example.com.txt
2019-10-01_16:54_another_recipient#example.com.txt
...
There is no such built-in configuration, no, but you can easily create your own custom transport that logs emails to files instead of sending them.
Here's a very basic example transport that extends the debug transport, and writes the data to a custom logging scope:
namespace App\Mailer\Transport;
use Cake\Log\LogTrait;
use Cake\Mailer\Email;
use Cake\Mailer\Transport\DebugTransport;
use Psr\Log\LogLevel;
class TestTransport extends DebugTransport
{
use LogTrait;
public function send(Email $email)
{
$data = parent::send($email);
$this->log(json_encode($data), LogLevel::DEBUG, ['scope' => ['emails']]);
return $data;
}
}
See also
Cookbook > Email > Using Transports > Creating Custom Transports

Specify an output message for successful test case

Each Assert allow to define error message which will be printed out in case of assert failure but can I somehow provide a message which will be outputed in case of successful test run?
I just use Console.WriteLine( "Your message here" ); which will be directed to the NUnit output so you can see Test passed messages
You can use the Assert.Pass utility method.
You've said that you want this information for use by your continuous integration environment. Presumably your CI environment is using the NUnit-console Runner?
If so then I recommend making use of the XML output generated from each test run, and example of which can be found here.
The XML output contains results for each and every test. You haven't said which CI server you're using, but it should be straightforward to build a step into your build process to import this XML and mine it for the stats you need.
You should handle the NUnit.Framework.SuccessException and print the message:
try
{
// assert
Assert.Pass(sOutput);
}
catch (SuccessException ex)
{
Console.WriteLine(ex.Message);
return;
}
As a result you should get the output message under clicking Output:

What is the best way to log errors in Zend Framework 1?

We built an app in Zend Framework (v1) and have not worked a lot in setting up error reporting and logging. Is there any way we could get some level or error reporting without too much change in the code? Is there a ErrorHandler plugin available?
The basic requirement is to log errors that happens within the controller, missing controllers, malformed URLs, etc.
I also want to be able to log errors within my controllers. Will using error controller here, help me identify and log errors within my controllers? How best to do this with minimal changes?
I would use Zend_Log and use the following strategy.
If you are using Zend_Application in your app, there is a resource for logging. You can read more about the resource here
My advice would be to choose between writing to a db or log file stream. Write your log to a db if you plan on having some sort of web interface to it, if not a flat file will do just fine.
You can setup the logging to a file with this simple example
resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.stream = APPLICATION_PATH "/../data/logs/application.log"
resources.log.stream.writerParams.mode = "a"
resources.log.stream.filterName = "Priority"
resources.log.stream.filterParams.priority = 4
Also, I would suggest sending Critical errors to an email account that is checked regularly by your development team. The company I work for sends them to errors#companyname.com and that forwards to all of the developers from production sites.
From what I understand, you can't setup a Mail writer via a factory, so the resource won't do you any good, but you can probably set it up in your ErrorController or Bootstrap.
$mail = new Zend_Mail();
$mail->setFrom('errors#example.org')
->addTo('project_developers#example.org');
$writer = new Zend_Log_Writer_Mail($mail);
// Set subject text for use; summary of number of errors is appended to the
// subject line before sending the message.
$writer->setSubjectPrependText('Errors with script foo.php');
// Only email warning level entries and higher.
$writer->addFilter(Zend_Log::WARN);
$log = new Zend_Log();
$log->addWriter($writer);
// Something bad happened!
$log->error('unable to connect to database');
// On writer shutdown, Zend_Mail::send() is triggered to send an email with
// all log entries at or above the Zend_Log filter level.
You will need to do a little work to the above example but the optimal solution would be to grab the log resource in your bootstrap file, and add the email writer to it, instead of creating a second log instance.
You can use Zend_Controller_Plugin_ErrorHandler . As you can see on the documentation page there is an example that checks for missing controller/action and shows you how to set the appropriate headers.
You can then use Zend_Log to log your error messages to disk/db/mail.