I've got a Silverstripe 3.1 site in development and want to write messages to the default log file - silverstripe.log
This is what we use to output variables or messages to the screen:
Debug::show($variable);
Debug::message("Debug message goes here");
What is the simplest way to output these to the silverstripe.log file? I've been looking through the documentation and can't find the correct approach: http://doc.silverstripe.com/framework/en/topics/debugging
You could do the following:
in mysite/_config.php
SS_Log::add_writer(new SS_LogFileWriter('silverstripe.log'), SS_Log::WARN, '>');
in your code:
SS_Log::log("Dammit, an issue with variable ".$var, SS_Log::WARN);
More at http://doc.silverstripe.com/framework/en/topics/error-handling
Also reading the code in framework/dev/Log.php will give you more insight on how priorities work.
PS: ensure to define 'silverstripe.log' in a folder writable by the apache user
The simplest solution is actually pretty close to your original attempt.
Debug::log("output");
This will output to a debug.log file in the site root.
Related
I'd like to debug some PHP code, but I guess printing a log to screen or file is fine for me.
How should I print a log in PHP code?
The usual print/printf seems to go to HTML output not the console.
I have Apache server executing the PHP code.
A lesser known trick is that mod_php maps stderr to the Apache log. And, there is a stream for that, so file_put_contents('php://stderr', print_r($foo, TRUE)) will nicely dump the value of $foo into the Apache error log.
error_log(print_r($variable, TRUE));
might be useful
You can use error_log to send to your servers error log file (or an optional other file if you'd like)
If you are on Linux:
file_put_contents('your_log_file', 'your_content');
or
error_log ('your_content', 3, 'your_log_file');
and then in console
tail -f your_log_file
This will show continuously the last line put in the file.
You need to change your frame of mind. You are writing PHP, not whatever else it is that you are used to write. Debugging in PHP is not done in a console environment.
In PHP, you have 3 categories of debugging solutions:
Output to a webpage (see dBug library for a nicer view of things).
Write to a log file
In session debugging with xDebug
Learn to use those instead of trying to make PHP behave like whatever other language you are used to.
Are you debugging on console? There are various options for debugging PHP.
The most common function used for quick & dirty debugging is var_dump.
That being said and out of the way, although var_dump is awesome and a lot of people do everything with just that, there are other tools and techniques that can spice it up a bit.
Things to help out if debugging in a webpage, wrap <pre> </pre> tags around your dump statement to give you proper formatting on arrays and objects.
Ie:
<div> some html code ....
some link to test
</div>
dump $tpl like this:
<pre><?php var_dump($tpl); ?></pre>
And, last but not least make sure if debugging your error handling is set to display errors. Adding this at the top of your script may be needed if you cannot access server configuration to do so.
error_reporting(E_ALL);
ini_set('display_errors', '1');
Good luck!
You can also write to a file like this:
$logFilePath = '../logs/debug.text';
ob_start();
// if you want to concatenate:
if (file_exists($logFilePath)) {
include($logFilePath);
}
// for timestamp
$currentTime = date(DATE_RSS);
// echo log statement(s) here
echo "\n\n$currentTime - [log statement here]";
$logFile = fopen($logFilePath, 'w');
fwrite($logFile, ob_get_contents());
fclose($logFile);
ob_end_flush();
Make sure the proper permissions are set so php can access and write to the file (775).
If you don't want to integrate a framework like Zend, then you can use the trigger_error method to log to the php error log.
Simply way is trigger_error:
trigger_error("My error");
but you can't put arrays or Objects therefore use
var_dump
You can use the php curl module to make calls to http://liveoutput.com/. This works great in an secure, corporate environment where certain restrictions in the php.ini exists that restrict usage of file_put_contents.
This a great tool for debugging & logging php: PHp Debugger & Logger
It works right out of the box with just 3 lines of code.
It can send messages to the js console for ajax debugging and can replace the error handler.
It also dumps information about variables like var_dump() and print_r(), but in a more readable format.
Very nice tool!
I have used many of these, but since I usually need to debug when developing, and since I develop on localhost, I have followed the advice of others and now write to the browser's JavaScript debug console (see http://www.codeforest.net/debugging-php-in-browsers-javascript-console).
That means that I can look at the web page which my PHP is generating in my browser & press F12 to quickly show/hide any debug trace.
Since I am constantly looking at the developer tools for debugger, CSS layout, etc, it makes sense to look at my PHP loggon there.
If anyone does decide to us that code, I made one minor change. After
function debug($name, $var = null, $type = LOG) {
I added
$name = 'PHP: ' . $name;
This is because my server side PHP generates HTML conatining JavaScript & I find it useful to distinguish between output from PHP & JS.
(Note: I am currently updating this to allow me to switch on & off different output types: from PHP, from JS, and database access)
I use cakephp so I use:
$this->log(YOUR_STRING_GOES_HERE, 'debug');
You can use:
<?php
echo '<script>console.log("debug log")</script>';
?>
You can use
<?php
{
AddLog("anypage.php","reason",ERR_ERROR);
}
?>
or if you want to print that statement in an log you can use
AddLog("anypage.php","string: ".$string,ERR_DEBUG_LOW);
I have used codeigniter framework.Have used its log part.
I had a query regarding configuring logs in the application.
As per the documentation they say that we can configure in app/config/php and filter the logs as debug,error or info messages.
But even after doing that, I am not able to filter the logs.
Wanted to know the reason for this or am i missing something.
Help would be highly appreciated.
You need to follow the following steps to keep log file.
Change the following line of your application/config/config.php file
$config['log_threshold'] = 4;
instead of
$config['log_threshold'] = 0;
This change will produce a log file in your application/logs directory with current date as file name.
Now if you want to write your custom log in this log file then write the following code:
log_message('ERROR','YOUR_MESSAGE_HERE');
Here you can use 'ERROR', 'DEBUG' etc.
Hope this will help you.
How can I print out some debug output, like the contents of an array variable? I'm using cakephp and eclipse and can't seem to be able to do this. I am not talking about logging errors, just print some variable value. It might be obvious and really easy to do, but I can't find a way to do it.
Thank you
There are no dumb questions for someone learning. So here are your options :
Anywhere in your code, place the following statement debug($var);.
This works in Controllers/Views and Models as well.
Alternative: use CakeLog::write(LOG_DEBUG, "..."); to log debug
values
To be complete, one should install the very helpful DebugKit plugin. Get it from here
First check in your core.php file inside config folder ..
debug mode is 2 or not..
Configure::write('debug', 2);
and you can print data , array ,
like this:-
debug($data);
pr($data);
print_r($data);
we have debuging kit also for cakephp, By this you can see your request, session, $this->data values,, url,
All you have to do is to use cakephp debug function like
debug($arrayname);
Before that you have to set debug mode to 2 at core.php under app/config folder
Here is more detail about the debug
I have created a test module name Mytest. While saving values from the module, I am getting a blank page and it saying "Sorry! Attempt to access restricted file. " . Do anyone know, why this happening. Any help on this is really appreciating.
The most likely cause for the vTiger error “Sorry! Attempt to access restricted file.” is the $root_directory value in the ‘config.inc.php’ is incorrect or misspelled.
In order to correct it follow the steps below:
Go to your vTigerCRM directory
Open “config.inc.php” with your favorite text editor
Go to line 86 and adjust $root_directory value to correct vTiger
directory. Note, that the directory must end with /. It should look
something like this – $root_directory = ‘/var/www/vtigercrm/’;
Also there is a problem with cache memory. So do check your cache file for template files. For that go to your vTigerCRM directory.
Then Go to Smarty->templates_c.
Here you will get list of cache files. Delete this file and check weather your problem is solved or not.
Don't worry about deletion of this file.
When trying to include files from your custom module, you will get these messages because Vtiger thinks you are including these files from a location they find rather unsafe.
To avoid this error you could use the standard way a module is used in Vtiger by navigating to it like so: ......./index.php?module=Mytest&action=index. Vtiger will include your module and now there is no need for you to include CRMEntity and other data or utils related files. It should all be available this way but make sure you are using the global statement for $current_user, $current_module etc though.
Another way is to edit the following functions located in utils/CommonUtils.php:
heckFileAccessForInclusion() and checkFileAccess()
Remove or comment out the die() in these functions to fix it.
In Save.php file, just add a line.
$focus->column_fields['assigned_user_id'] = '';
before the
if($_REQUEST['assigntype'] == 'U') {
$focus->column_fields['assigned_user_id'] = $_REQUEST['assigned_user_id'];
} elseif($_REQUEST['assigntype'] == 'T') {
$focus->column_fields['assigned_user_id'] = $_REQUEST['assigned_group_id'];
}
To second what caspersky said:
Go to /include/database/PearDatabase.php and add
$adb->setDebug(true); right after $adb->connect();
I just wrote a module and received this error and it was because the record could not save because I left out:
$moduleInstance->setEntityIdentifier($fieldInstance);
Check out file permissions and file path it's trying to refer.
If you want to debug more set $adb->setDebug(true) in your index file and checkout for the errors.
A couple of things spring to mind:
Have you actually created the modules/CustomeModule directory and populated
it? (Using the template in vtlib/ModuleDir/5.4.0 and then editing the
filenames and class of CustomeModule.php)
Check the case of your module class definition, e.g. class CustomeModule
vs. class Customemodule
If you are using any version control or symlinks in the development
of your modules/Mytest code then this can trigger the "Sorry! Attempt
to access restricted file." messages.
In module setup script make sure you have added this lines.
$module->initTables();
$module->initWebservice();
Check that all language files exist.
The user module allows the admin user to configure a user's language even though the language file is not present on disk.
To quickly verify this is indeed the issue :-
- Edit the include/utils/CommonUtils.php and print the $realfilepath variable ,and comment out the die();
- In the database, "select distinct language from xxx_users";
You can fix this by downloading the required files.
As a quick fix (read:hack):-
- go to the include/language directory
- copy an existing language file as the required one. (may not always work - for example en_us to en_gb is great, but en_us to sp_es is not)
It seems you did not set write permission for Smarty folder
Probably a file is missing in your vtiger install.
To find out which one is mission you would need to edit the include/utils/CommonUtils.php file. Open it with a text editor, go around line 2755 and add the following
echo “REAL: $realfilepath, ROOT: $rootdirpath”;
Before die(Sorry....)
This would print on the screen which one is the missing file.
Sometimes this error is caused by an nonexistent module, what I mean here is that vtiger thinks you have a module but the files are not in there (might be caused by a bad migration to a new server).
Disable some modules and try again until you find which module is broken.
In my case the broken module was VGS.
I solved this on vtiger 7.3.. (maybe it works for other vesion)
I went to users permission on vtiger inside configuration settings and update tham all again with the same settings .. and got them to a more default settings .. them all users appeared back and I was able to create new users ..change password again.
I suggest logging out and maybe forcing refresh and waiting a little to make it work .
I would like to implement a simple plugin system for my script. I'll be including the files of the plugins which the user selects as available.
Currently, if any of the files has some parse error, it causes the complete thing to go down, until I hack into the db to remove that plugin entry.
Is there any easy way to do this? (Somehow check the files for parse errors atleast)?
You could make a HTTP request to a PHP file that tries to include the file, and outputs "OK" at the end.
Check_include.php:
$filename = $_GET["plugin"];
$filename = sanitize_filename($filename); // Here you make sure only plugins can be loaded
include ($filename);
echo "OK";
then, in your admin panel or whatever, call
file_get_contents("http://mydomain/check_include.php?plugin=my_new_plugin");
and see whether the result contains the word "OK". If the result is okay, there was no fatal error, and you can activate the plugin; otherwise, you can even output the error message that got output when trying to include it.
WordPress uses a variation of this involving an IFRAME to check new plugins.
Note: Making a HTTP request is expensive. You should under no circumstances do this every time the plugin is included, but only once on installation. Use a flag of some sort (e.g. a underscore _ in front of the plugin name) to enable / disable plugins.
No. A parse error is a fatal error that you cannot recover from.