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 trial

PHP Building Websites with PHP Dependency Management Namespacing

Niall Maher
Niall Maher
16,985 Points

Just 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
STAFF
Alena Holligan
Treehouse Teacher

Niall Maher ,

You 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 :)

Ted Sumner,

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 :)

Alena Holligan
Alena Holligan
Treehouse Teacher

These 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 :)

Now I feel silly. lol.

I never saw the errors in Workspaces. How do I see them?

Niall Maher
Niall Maher
16,985 Points

I 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
Niall Maher
16,985 Points

Hello world is showing in the console when I use "php index.php"

Alena Holligan
Alena Holligan
Treehouse Teacher

for more error handling add the following lines to the top of your file within the PHP codeblock

error_reporting(E_ALL);
ini_set("display_errors", 1);

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
Niall Maher
16,985 Points

I 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

that fork works for me in Workspaces....

Alena Holligan
Alena Holligan
Treehouse Teacher

Niall Maher, when I fork your workspace it works for me. If you are still having issues, shoot me an email

The 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
Niall Maher
16,985 Points

Aaah I forgot to take it back when I was resetting back to my original code!

I 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
Niall Maher
16,985 Points

Good eye! Thanks :) But still getting the same error :/

I 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.

Also, please post your composer.json file.

Niall Maher
Niall Maher
16,985 Points

That works fine! But still original error :P

{
    "name": "root/workspace",
    "authors": [
        {
            "name": "Niall Maher",
            "email": "nialljoemaher@gmail.com"
        }
    ],
    "require": {
        "monolog/monolog": "~1.13"
    }
}

Interesting. 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
Niall Maher
16,985 Points

Still nothing, quite frustrating :P I want to make my website ;)

I 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
Niall Maher
16,985 Points

I'm using workspaces! I'll let you know shortly

Oh. 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.

I 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
Niall Maher
16,985 Points

No but I will have a look at trying to setup one now :) Thanks a million sorry for the headache ;)

I sent an email to support for help on this.

Here is my functioning snapshot. https://w.trhou.se/5yvz8j0z1a. I will look at your with fresh eyes.

Can 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

I 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

It 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
STAFF
Alena Holligan
Treehouse Teacher

Please take a snapshot of your workspace and share (near the preview button)

Alena,

here is the snapshot

https://w.trhou.se/v8d05upg5n

Alena Holligan
Alena Holligan
Treehouse Teacher

Pat Costa sorry for the late reply. Try deleting the vendor/monolog folder then running composer update again. That seemed to fix it for me.