Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialNiall Maher
16,985 PointsJust making sure my code's okay as I am getting a server error
Here's my code the problem is I'm getting the following error when i run it "Failed to load resource: the server responded with a status of 500 (Internal Server Error)"
I think my code is correct but I don't want to move on if I'm not doing it right :P
<?php
require 'vendor/autoload.php';
date_default_timezone_set ('Europe/Dublin');
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$log = new Logger('name');
$log->pushHandler(new StreamHandler('app.txt', Mogger::WARNING));
$log->addWarning('oh nose');
echo 'hello world';
?>
15 Answers
Alena Holligan
Treehouse TeacherYou are so close! :) like most problems you just have a simple miss spelling.
if you go to your console and type "php index.php" it should give you the following error:
PHP Fatal error: Class 'Mogger' not found in /home/treehouse/workspace/index.php on line 11
if you rename "Mogger" to "Logger" it should work for you. I tested out your code myself :)
Same thing with your code. If you run in the browser you should get the following errors:
PHP Notice: date_default_timezone_set(): Timezone ID 'America/Los Angeles' is invalid in /home/t
reehouse/workspace/index.php on line 4
PHP Fatal error: Class 'StreamHandler' not found in /home/treehouse/workspace/index.php on line
11
Timezone issue is just that it should be "Los_Angeles" with and underscore instead of space Class 'StreamHandler' not found is because this line is misspelled:
use Monolog\Handler\SreamHandler;
just missing a 't'
use Monolog\Handler\StreamHandler;
Thank you Ted for all your work on this. You two are doing great :) We will be introducing more debugging courses soon :)
Ted Sumner
Courses Plus Student 17,967 PointsThe first problem is that you have two <?php
. Once I removed the test code at the beginning code I was able to determine that it breaks at the addWarning line. Still troubleshooting.
Niall Maher
16,985 PointsAaah I forgot to take it back when I was resetting back to my original code!
Ted Sumner
Courses Plus Student 17,967 PointsI don't think it likes Mogger in this line:
<?php
$log->pushHandler(new StreamHandler('app.txt', Mogger::WARNING));
It would probably like Logger better.
Niall Maher
16,985 PointsGood eye! Thanks :) But still getting the same error :/
Ted Sumner
Courses Plus Student 17,967 PointsI think you need to figure it out prior to moving on. Do other PHP sites work in your environment? You can test it with something simple like this:
<?php
$name = 'Niall';
if ($name === 'ted') {
echo "Your name is ted";
} else {
echo "Your name is not ted. It is $name.";
}
If you still get a server error, you should try restarting your server.
Ted Sumner
Courses Plus Student 17,967 PointsAlso, please post your composer.json file.
Niall Maher
16,985 PointsThat works fine! But still original error :P
{
"name": "root/workspace",
"authors": [
{
"name": "Niall Maher",
"email": "nialljoemaher@gmail.com"
}
],
"require": {
"monolog/monolog": "~1.13"
}
}
Ted Sumner
Courses Plus Student 17,967 PointsInteresting. My json file has "monolog/monolog": "^1.13"
, not `"~1.13". You might change that and see if it makes a difference.
Since the basic PHP code is working, you know the server error is related to your setup in this file. I fear you will continue to get this error if you continue without figuring it out. You can also start by commenting out 1/2 of the monologger code and seeing if you still get the error. Keep cutting it in half until you figure out what exactly is causing the error. then we can figure out how to fix it.
Niall Maher
16,985 PointsStill nothing, quite frustrating :P I want to make my website ;)
Ted Sumner
Courses Plus Student 17,967 PointsI just ran your code in my system and it worked once I fixed Mogger. Mogger kicked an error and referenced the line it was on. Therefore, you error is in your Logger files in the vendor folder, in the server itself, or in some other file.
Try commenting out all of the logger code and see if you get hello world. if not:
Try restarting your server. (I assume you are using a local server like XAMPP)
If you do get hello world, try uncommenting 1/2 your code and see if the error comes back. Keep narrowing down the error until you can isolate a line or lines. You may have a problem with your logger files in the vendor folder, so you may want to the redownload them following the instructions in the video.
Niall Maher
16,985 PointsI'm using workspaces! I'll let you know shortly
Ted Sumner
Courses Plus Student 17,967 PointsOh. make a snapshot of the space by clicking on the camera icon and following the steps until you get a url you can post here. Then I can fork your space and work with the same file.
Niall Maher
16,985 PointsYou are an absolute gent! https://w.trhou.se/2camktan2i
Ted Sumner
Courses Plus Student 17,967 PointsI don't know. I cannot make it work in Workspaces either. I tried deleting monolog and reinstalling it with version 1.17 and building my own new workspace. i was able to download it, but it just does not work. I looked at the read me in the vendor/monolog/monolog folder and it appears correctly coded. Do you have a local development environment set up on your computer?
Niall Maher
16,985 PointsNo but I will have a look at trying to setup one now :) Thanks a million sorry for the headache ;)
Ted Sumner
Courses Plus Student 17,967 PointsI sent an email to support for help on this.
Ted Sumner
Courses Plus Student 17,967 PointsHere is my functioning snapshot. https://w.trhou.se/5yvz8j0z1a. I will look at your with fresh eyes.
Ervin Selimovic
3,962 PointsCan you please help me with this kind of error
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'The stream or file "app.txt" could not be opened: failed to open stream: Permission denied' in /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:87 Stack trace: #0 /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37): Monolog\Handler\StreamHandler->write(Array) #1 /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Logger.php(289): Monolog\Handler\AbstractProcessingHandler->handle(Array) #2 /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Logger.php(341): Monolog\Logger->addRecord(300, 'Foo', Array) #3 /home/treehouse/workspace/index.php(11): Monolog\Logger->addWarning('Foo') #4 {main} thrown in /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php on line 87
Pat Costa
4,629 PointsI have the same issue: Here is my posting elsewhere as I was not given an option to post in this section https://teamtreehouse.com/community/apptxt-messages-do-not-appear
Pat Costa
4,629 PointsIt appears that for whatever reason the error message is not longer app.txt related. I am now getting an error message of Fatal error: Class 'Monolog\Logger' not found in /home/treehouse/workspace/index.php on line 8
with the code
<?php require 'vendor/autoload.php'; date_default_timezone_set ('Europe/Dublin');
use Monolog\Logger; use Monolog\Handler\StreamHandler;
$log = new Logger('name');
$log->pushHandler(new StreamHandler('app.txt', Logger::WARNING));
$log->addWarning('Foo');
echo 'hello world';
?>;
Alena Holligan
Treehouse TeacherPlease take a snapshot of your workspace and share (near the preview button)
Pat Costa
4,629 PointsAlena Holligan
Treehouse TeacherPat Costa sorry for the late reply. Try deleting the vendor/monolog folder then running composer update again. That seemed to fix it for me.
Alena Holligan
Treehouse TeacherAlena Holligan
Treehouse TeacherThese are the types of things even seasoned programmers run into. Learning to debug helps, but it's also great to have someone to look over your code for you too :)
Ted Sumner
Courses Plus Student 17,967 PointsTed Sumner
Courses Plus Student 17,967 PointsNow I feel silly. lol.
Ted Sumner
Courses Plus Student 17,967 PointsTed Sumner
Courses Plus Student 17,967 PointsI never saw the errors in Workspaces. How do I see them?
Niall Maher
16,985 PointsNiall Maher
16,985 PointsI changed my code to Logger but still get no result still the original error in chrome debug. But if I run all my code in my local server it's fine?
Niall Maher
16,985 PointsNiall Maher
16,985 PointsHello world is showing in the console when I use "php index.php"
Alena Holligan
Treehouse TeacherAlena Holligan
Treehouse Teacherfor more error handling add the following lines to the top of your file within the PHP codeblock
Niall Maher you still have and extra opening php tag. In the workspace console, type "php index.php" and press enter. You should see the following error:
PHP Parse error: syntax error, unexpected '<' in /home/treehouse/workspace/index.php on line 8
Line 8 is another <?php tag, remove that line
Niall Maher
16,985 PointsNiall Maher
16,985 PointsI don't see the extra ">" https://w.trhou.se/mv9kkrwim1
The error I'm getting is:
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'The stream or file "app.txt" could not be opened: failed to open stream: Permission denied' in /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:87 Stack trace: #0 /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37): Monolog\Handler\StreamHandler->write(Array) #1 /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Logger.php(289): Monolog\Handler\AbstractProcessingHandler->handle(Array) #2 /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Logger.php(341): Monolog\Logger->addRecord(300, 'oh nose', Array) #3 /home/treehouse/workspace/index.php(16): Monolog\Logger->addWarning('oh nose') #4 {main} thrown in /home/treehouse/workspace/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php on line 87
Ted Sumner
Courses Plus Student 17,967 PointsTed Sumner
Courses Plus Student 17,967 Pointsthat fork works for me in Workspaces....
Alena Holligan
Treehouse TeacherAlena Holligan
Treehouse TeacherNiall Maher, when I fork your workspace it works for me. If you are still having issues, shoot me an email